Codex tools: Log in / create account
Languages: English • Türkçe • (Add your language)
Contents |
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 ); ?>
Before calling wp_insert_post() it is necessary to create an object (typically 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(); $my_post['post_title'] = 'My post'; $my_post['post_content'] = 'This is my post.'; $my_post['post_status'] = 'publish'; $my_post['post_author'] = 1; $my_post['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' => ''
);
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.
IMPORTANT: Setting a value for $post['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['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. 'page_template' => [ <template file> ] //Sets the template for the page. 'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 'ping_status' => [ ? ] //Ping status? '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' ] //Set the status of the new post. 'post_title' => [ <the title> ] //The title of your post. 'post_type' => [ 'post' | 'page' ] //Sometimes you want to post a page. 'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 'to_ping' => [ ? ] //? );
The ID of the post if the post is successfully added to the database. Otherwise returns 0.
wp_update_post(), wp_delete_post(), wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment()