WordPress:Function Reference/wp insert post
Description
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).
Usage
%%% <?php wp_insert_post( $post ); ?> %%%
Example
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; // 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' => '' );
Parameters
Return
The ID of the post if the post is successfully added to the database. Otherwise returns 0.
Related
wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment()
This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.