描述
該函數(shù)可在wordpress數(shù)據(jù)庫(kù)中插入文章(及頁(yè)面)。它可以進(jìn)行處理變量,檢查操作,填充日期/時(shí)間等缺失變量等工作。該函數(shù)以對(duì)象作為變量,返回已創(chuàng)建文章的編號(hào)(出錯(cuò)時(shí)返回0)。
使用方法
- <?php?wp_insert_post(?$post,?$wp_error?);??>
參數(shù)
$post
(array) (必需) 一個(gè)文章對(duì)象. 與數(shù)據(jù)庫(kù)wp_posts表中的字段一一對(duì)應(yīng)
默認(rèn): 無(wú)
重要: 如果設(shè)置$post[‘ID’]的值,將不會(huì)創(chuàng)建 這個(gè)ID的文章. 設(shè)置這個(gè)值將會(huì)更新這個(gè)ID的文章. 簡(jiǎn)單的說(shuō),創(chuàng)建一個(gè)文章 $post[‘ID’] 必須為空或不設(shè)置這個(gè)值。
- $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
(布爾型) (可選) 失敗時(shí)是否返回WP_Error對(duì)象
默認(rèn): false
返回的值
若文章成功加入數(shù)據(jù)庫(kù),返回文章編號(hào)。否則返回0.
使用方法
- <?php?wp_insert_post(?$post,?$wp_error?);??>
例子
// 創(chuàng)建一個(gè)文章對(duì)象
- $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?);
安全
函數(shù)會(huì)自動(dòng)過(guò)濾和檢查文章信息的合法性,不需要用戶自己來(lái)額外處理
源碼位置
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;
- }