This function inserts posts (and pages) in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an object as its argument and returns the post ID of the created post (or 0 if there is an error).
<?php wp_insert_post( $post, $wp_error ); ?>
IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all.
The contents of the post array can depend on how much (or little) you want to trust the defaults. Here is a list with a short description of all the keys you can set for a post:
$post = array( 'ID' => [ <post id> ] //Are you updating an existing post? 'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs. 'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 'ping_status' => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off 'pinged' => [ ? ] //? 'post_author' => [ <user ID> ] //The user ID number of the author. 'post_category' => [ array(<category id>, <...>) ] //Add some categories. 'post_content' => [ <the text of the post> ] //The full text of the post. 'post_date' => [ Y-m-d H:i:s ] //The time post was made. 'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT. 'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs. 'post_name' => [ <the name> ] // The name (slug) for your post 'post_parent' => [ <post ID> ] //Sets the parent of the new post. 'post_password' => [ ? ] //password for post? 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //Set the status of the new post. 'post_title' => [ <the title> ] //The title of your post. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //You may want to insert a regular post, page, link, a menu item or some custom post type 'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 'to_ping' => [ ? ] //? 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. );
NOTE: The page_template key was removed from the $post table and is now located in the wp_postmeta table. To set the page template of a page, use update_post_meta instead with a meta key of _wp_page_template.
NOTE 2: The post_status is documented at Post Status Transitions.
NOTE 3: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post.
NOTE 4: If the current user doesn't have the capability to work with a custom taxonomy then using tax_input to add a term won't work. You will need to use wp_set_object_terms().
The ID of the post if the post is successfully added to the database. On failure, it returns 0 if $wp_error is set to false, or a WP_Error object if $wp_error is set to true.
<?php wp_insert_post( $post, $wp_error ); ?>
Before calling wp_insert_post() it is necessary to create an array to pass the necessary elements that make up a post. The wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.
The user can provide more elements than are listed here by simply defining new keys in the database. The keys should match the names of the columns in the wp_posts table in the database.
// Create post 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) ); // Insert the post into the database wp_insert_post( $my_post );
The default list referred to above is defined in the function body. It is as follows:
$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);
Categories need to be passed as an array of integers that match the category IDs in the database. This is the case even where only one category is assigned to the post.
See also: wp_set_post_terms()
wp_insert_post() passes data through sanitize_post(), which itself handles all necessary sanitization and validation (kses, etc.).
As such, you don't need to worry about that.
// Create post object $my_post = array( 'post_title' => wp_strip_all_tags( $_POST['post_title'] ), 'post_content' => $_POST['post_content'], 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array( 8,39 ) ); // Insert the post into the database wp_insert_post( $my_post );
wp_insert_post() is located in