描述
该函数可在wordpress数据库中插入文章(及页面)。它可以进行处理变量,检查操作,填充日期/时间等缺失变量等工作。该函数以对象作为变量,返回已创建文章的编号(出错时返回0)。
使用方法
- <?php wp_insert_post( $post, $wp_error ); ?>
参数
$post
(array) (必需) 一个文章对象. 与数据库wp_posts表中的字段一一对应
默认: 无
重要: 如果设置$post[‘ID’]的值,将不会创建 这个ID的文章. 设置这个值将会更新这个ID的文章. 简单的说,创建一个文章 $post[‘ID’] 必须为空或不设置这个值。
- $post = array(
-
- 'ID' => [ <post id> ]
-
- 'menu_order' => [ <order> ]
-
- 'comment_status' => [ 'closed' | 'open' ]
-
- 'ping_status' => [ 'closed' | 'open' ]
-
- 'pinged' => [ ? ]
-
- 'post_author' => [ <user ID> ]
-
- 'post_category' => [ array(<category id>, <...>) ]
-
- 'post_content' => [ <the text of the post> ]
-
- 'post_date' => [ Y-m-d H:i:s ]
-
- 'post_date_gmt' => [ Y-m-d H:i:s ]
-
- 'post_excerpt' => [ <an excerpt> ]
-
- 'post_name' => [ <the name> ]
-
- 'post_parent' => [ <post ID> ]
-
- 'post_password' => [ ? ]
-
- 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ]
-
- 'post_title' => [ <the title> ]
-
- 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ]
-
- 'tags_input' => [ '<tag>, <tag>, <...>' ]
-
- 'to_ping' => [ ? ]
-
- 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ]
-
- );
$wp_error
(布尔型) (可选) 失败时是否返回WP_Error对象
默认: false
返回的值
若文章成功加入数据库,返回文章编号。否则返回0.
使用方法
- <?php wp_insert_post( $post, $wp_error ); ?>
例子
// 创建一个文章对象
- $my_post = array(
- 'post_title' => 'My post',
- 'post_content' => 'This is my post.',
- 'post_status' => 'publish',
- 'post_author' => 1,
- 'post_category' => array(8,39)
- );
- wp_insert_post( $my_post );
安全
函数会自动过滤和检查文章信息的合法性,不需要用户自己来额外处理
源码位置
wp_insert_post() 位于 wp-includes/post.php
- function wp_insert_post($postarr, $wp_error = false) {
- global $wpdb, $wp_rewrite, $user_ID;
- $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
- 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
- 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
- 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
- 'post_content' => '', 'post_title' => '');
- $postarr = wp_parse_args($postarr, $defaults);
- unset( $postarr[ 'filter' ] );
- $postarr = sanitize_post($postarr, 'db');
-
- extract($postarr, EXTR_SKIP);
-
- $update = false;
- if ( !emptyempty($ID) ) {
- $update = true;
- $previous_status = get_post_field('post_status', $ID);
- } else {
- $previous_status = 'new';
- }
- if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) && ('attachment' != $post_type) ) {
- if ( $wp_error )
- return new WP_Error('empty_content', __('Content, title, and excerpt are emptyempty.'));
- else
- return 0;
- }
- if ( emptyempty($post_type) )
- $post_type = 'post';
- if ( emptyempty($post_status) )
- $post_status = 'draft';
- if ( !emptyempty($post_category) )
- $post_category = array_filter($post_category);
-
- if ( emptyempty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
-
- if ( 'post' == $post_type && 'auto-draft' != $post_status )
- $post_category = array( get_option('default_category') );
- else
- $post_category = array();
- }
- if ( emptyempty($post_author) )
- $post_author = $user_ID;
- $post_ID = 0;
-
- if ( $update ) {
- $post_ID = (int) $ID;
- $guid = get_post_field( 'guid', $post_ID );
- $post_before = get_post($post_ID);
- }
-
- if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )
- $post_name = '';
-
-
- if ( emptyempty($post_name) ) {
- if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
- $post_name = sanitize_title($post_title);
- else
- $post_name = '';
- } else {
- $post_name = sanitize_title($post_name);
- }
-
- if ( emptyempty($post_date) || '0000-00-00 00:00:00' == $post_date )
- $post_date = current_time('mysql');
- if ( emptyempty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
- if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
- $post_date_gmt = get_gmt_from_date($post_date);
- else
- $post_date_gmt = '0000-00-00 00:00:00';
- }
- if ( $update || '0000-00-00 00:00:00' == $post_date ) {
- $post_modified = current_time( 'mysql' );
- $post_modified_gmt = current_time( 'mysql', 1 );
- } else {
- $post_modified = $post_date;
- $post_modified_gmt = $post_date_gmt;
- }
- if ( 'publish' == $post_status ) {
- $now = gmdate('Y-m-d H:i:59');
- if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) )
- $post_status = 'future';
- } elseif( 'future' == $post_status ) {
- $now = gmdate('Y-m-d H:i:59');
- if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) )
- $post_status = 'publish';
- }
- if ( emptyempty($comment_status) ) {
- if ( $update )
- $comment_status = 'closed';
- else
- $comment_status = get_option('default_comment_status');
- }
- if ( emptyempty($ping_status) )
- $ping_status = get_option('default_ping_status');
- if ( isset($to_ping) )
- $to_ping = preg_replace('|\s+|', "\n", $to_ping);
- else
- $to_ping = '';
- if ( ! isset($pinged) )
- $pinged = '';
- if ( isset($post_parent) )
- $post_parent = (int) $post_parent;
- else
- $post_parent = 0;
-
- $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
- if ( isset($menu_order) )
- $menu_order = (int) $menu_order;
- else
- $menu_order = 0;
- if ( !isset($post_password) || 'private' == $post_status )
- $post_password = '';
- $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
-
- $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );
- $data = apply_filters('wp_insert_post_data', $data, $postarr);
- $data = stripslashes_deep( $data );
- $where = array( 'ID' => $post_ID );
- if ( $update ) {
- do_action( 'pre_post_update', $post_ID );
- if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
- if ( $wp_error )
- return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
- else
- return 0;
- }
- } else {
- if ( isset($post_mime_type) )
- $data['post_mime_type'] = stripslashes( $post_mime_type );
-
- if ( !emptyempty($import_id) ) {
- $import_id = (int) $import_id;
- if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
- $data['ID'] = $import_id;
- }
- }
- if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
- if ( $wp_error )
- return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
- else
- return 0;
- }
- $post_ID = (int) $wpdb->insert_id;
-
- $where = array( 'ID' => $post_ID );
- }
- if ( emptyempty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
- $data['post_name'] = sanitize_title($data['post_title'], $post_ID);
- $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
- }
- if ( is_object_in_taxonomy($post_type, 'category') )
- wp_set_post_categories( $post_ID, $post_category );
- if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
- wp_set_post_tags( $post_ID, $tags_input );
-
- if ( !emptyempty($tax_input) ) {
- foreach ( $tax_input as $taxonomy => $tags ) {
- $taxonomy_obj = get_taxonomy($taxonomy);
- if ( is_array($tags) )
- $tags = array_filter($tags);
- if ( current_user_can($taxonomy_obj->cap->assign_terms) )
- wp_set_post_terms( $post_ID, $tags, $taxonomy );
- }
- }
- $current_guid = get_post_field( 'guid', $post_ID );
- if ( 'page' == $data['post_type'] )
- clean_page_cache($post_ID);
- else
- clean_post_cache($post_ID);
-
- if ( !$update && '' == $current_guid )
- $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
- $post = get_post($post_ID);
- if ( !emptyempty($page_template) && 'page' == $data['post_type'] ) {
- $post->page_template = $page_template;
- $page_templates = get_page_templates();
- if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
- if ( $wp_error )
- return new WP_Error('invalid_page_template', __('The page template is invalid.'));
- else
- return 0;
- }
- update_post_meta($post_ID, '_wp_page_template', $page_template);
- }
- wp_transition_post_status($data['post_status'], $previous_status, $post);
- if ( $update ) {
- do_action('edit_post', $post_ID, $post);
- $post_after = get_post($post_ID);
- do_action( 'post_updated', $post_ID, $post_after, $post_before);
- }
- do_action('save_post', $post_ID, $post);
- do_action('wp_insert_post', $post_ID, $post);
- return $post_ID;
- }