Description
This function inserts an article (and page) into the database. It can handle variables, check operations, fill in missing variables such as date/time, and so on. The function takes an object as a variable, returning the number of the created article (0 On error).
"How to use"
<?php wp_insert_post ($post, $wp _error);?>
Parameters
$post
(array) (required) An article object. Corresponds to field one by one in the database wp_posts table
Default: None
IMPORTANT: If you set the value of $post[' ID '], the article for this ID will not be created. Setting this value will update the article for this ID. Simply put, create an article $post [' ID '] must be empty or not set this value.
$post = array(' id ' = [<post id>]//need to update the article number ' menu_order ' = [<order>]//If the new article is a page, set the display order ' comment_status ' = [ ' Closed ' | ' Open '//comment status, ' closed ' comments off. ' Ping_status ' = [' Closed ' | ' Open '//ping status, ' closed ' off pingbacks and trackbacks ' pinged ' = [?]//The article was ping to the address ' post_author ' = [<user ID&G T ]//author number ' post_category ' = [Array (<category id>, <...>)]//Article Collation array ' post_content ' = = [<the text of T He post>]//article content, required ' post_date ' = [y-m-d h:i:s]//Article edit date ' post_date_gmt ' = [y-m-d h:i:s]//Article Edit GMT Date ' POST_EX Cerpt ' = [<an excerpt>]//summary information ' post_name ' = [<the name>]//(slug) Article alias ' post_parent ' = [<p OST id>]//The parent article number of the new article ' Post_password ' = [?]//Article Browse password ' post_status ' + [' draft ' | ' Publish ' | ' Pending ' | ' Future ' | ' Private '///new article status ' post_title ' = [<the title>]//article title, required ' post_type ' = [' Post ' | ' Page ' | ' Link ' | ' Nav_menu_item ' | Custom Post Type]//article type: articles, pages, links, menus, other custom types ' tags_iNput ' = [' <tag>, <tag>, <...> ']//Tag string ' to_ping ' = [?]//The article needs to ping the address ' tax_input ' and [AR Ray (' taxonomy_name ' = = Array (' term ', ' term2 ', ' term3 ')]//Additional annotation arrays); $WP _error
(Boolean type) (optional) whether to return a Wp_error object if it fails
Default: False
"Returned value"
If the article successfully joins the database, return the article number. Otherwise, 0 is returned.
"How to use"
<? php wp_insert_post( $post,);? >
Example
Create an Article Object
$my _post = array(' post_title ' = ' my post ', ' post_content ' = ' This is my post. ' Post_status ' = ' publish ', ' Post_author ' = 1, ' post_category ' = = Array (8,39));//Warehousing wp_insert_post ($my _post);
Security
The function automatically filters and checks the legality of the article information and does not require the user to handle the additional
"Source Location"
Wp_insert_post () is located in wp-includes/post.php
/** * Insert a post. * If the $postarr parameter have ' ID ' set to a value and then post would be updated. * can set the Post date manually, but setting the values for ' post_date ' * and ' post_date_gmt ' keys. You can close the comments or open the comments by * Setting the value for ' comment_status ' key. * * The defaults for the parameter $postarr be: * ' Post_status ' –default is ' draft '. * ' Post_type ' –default is ' post '. * ' Post_author ' –default is current user ID ($user _id). The ID of the user who added the post. * ' Ping_status ' –default is the value of ' default_ping_status ' option. * Whether the attachment can accept pings. * ' post_parent ' –default is 0. Set This is the post it belongs to, if any. * ' Menu_order ' –default is 0. The order it is displayed. * ' to_ping ' –whether to ping. * ' pinged ' –default is empty string. * ' Post_password ' –default is empty string. The password to access thE attachment. * ' GUID ' –global Unique ID for referencing the attachment. * ' post_content_filtered ' –post content filtered. * ' Post_excerpt ' –post excerpt. * * @since 1.0.0 * @uses $wpdb * @uses $wp _rewrite * @uses $user _id * @uses do_action () Calls ' pre_post_update ' on post ID If this was an update. * @uses do_action () Calls ' edit_post ' action on the post ID and post data if this is an update. * @uses do_action () Calls ' save_post ' and ' wp_insert_post ' on Post ID and post data just before returning. * @uses apply_filters () Calls ' wp_insert_post_data ' passing $data, $postarr prior to database update or INSERT. * @uses wp_transition_post_status () * * @param array $postarr Elements that make up post to insert. * @param bool $WP _error Optional. Allow return of Wp_error on failure. * @return int| Wp_error the value 0 or wp_error on failure. The post ID on success. */function wp_insert_post ($postarr, $wp _error = False) {Global $wpdb, $wp _rewrite, $user _id; $defaults = Array (' post_status ' = ' draft ', ' post_type ' = ' post ', ' post_author ' = = ' _id ', ' ping_status ' => $user ; 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 ' and '); $postarr = Wp_parse_args ($postarr, $defaults); unset ($postarr [' Filter '] ); $postarr = Sanitize_post ($postarr, ' db '); Export array as variables extract ($postarr,EXTR_SKIP); Are we updating or creating? $update = False; if (!empty ($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 _errorreturn new Wp_error (' Empty_content ', __ (' content, title, and excerpt are empty. '))); else return 0; } if (Empty ($post _type)) $post _type = ' post '; if (Empty ($post _status)) $post _status = ' draft '; if (!empty ($post _category)) $post _category = Array_filter ($post _category); Filter out empty terms//Make sure we set a valid category. if (Empty ($post _category) | | 0 = = COUNT ($post _category) | |!is_array ($post _category) {//' post ' requires at least one category. if (' post ' = = $post _type && ' auto-draft '! = $post _status) $post _category = Array (get_option (' default_category ')) ); else $post _category = array(); } if (Empty ($post _author)) $post _author = $user _id; $post _id = 0; Get the post ID and GUID if ($update) {$post _id = (int) $ID; $guid = Get_post_field (' GUID ', $post _id); $post _before = get_post ($post _id); }//Don ' t allow contributors to set the post slug for pending review posts if (' pending ' = = $post _status &&!cur Rent_user_can (' publish_posts ')) $post _name = "; Create a valid post name. Drafts and pending posts is allowed to has an empty//post name. if (Empty ($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 The Post date is empty (due to have been new or a draft) and status is not ' draft ' or ' pending ', set date to Nowif (Empty ($post _date) | | ' 0000-00-00 00:00:00 ' = = $post _date) $post _date = current_time (' mysql '); if (Empty ($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 (Empty ($comment _status)) {if ($update) $comment _status = ' closed '; else $comment _status = get_option (' Default_comment_status ');} if (Empty ($ping _status)) $ping _status = get_option (' default_ping_status '); if (Isset ($to _ping) $to _ping = preg_replace (' |\s+| ', "\ n", $to _ping), else $to _ping = "; Isset ($pinged) $pinged = '; if (Isset ($post _parent) $post _parent = (int) $post _parent;else $post _parent =0;//Check the post_parent to see if it'll cause a hierarchy loop $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);//Expected_slashed ( everything!) $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) Returnnew wp_error (' Db_update_error ', __ (' Could not update post in the database '), $wpdb->last_error); Elsereturn0;}} Else{if (Isset ($post _mime_type)) $data [' Post_mime_type ']= stripslashes ($post _mime_type);//This isn ' t in the update//If there is a suggested ID, use It if not already presentif (!empty ($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) returnnew wp_error (' Db_insert_error ', __ (' Could Not insert post into the database '), $wpdbLast_error); elsereturn0;} $post _id = (int) $wpdb->insert_id;//Use the newly generated $post _id $where = Array (' ID ' = + $post _id);} if (Empty ($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);//New-style support for all custom Taxonomiesif (!empty ($tax _input)) { foreach ($tax _input as $taxonomy = + $tags) {$taxonomy _obj = get_taxonomy ($taxonomy); if (Is_array ($tags))//array = HIE rarchical, String = non-hierarchical. $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);//Set guidif (! $update && "= = $current _guid) $wpdb->update ($wpdb->posts, Array (' guid ' = = Get_permalink ($post _id)), $where ); $post = Get_post ($post _id), if (!empty ($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) returnnew wp_error (' Invalid_page_template ', __ (' the page Template is invalid. ' ) ); elsereturn0;} 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;}
WordPress Code Insert article function: Wp_insert_post