Codex

Function Reference/wp insert post

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.

If you are writing a plugin and want to insert posts, use this function. Just create an object and feed it to wp_insert_post like this:


// create post object
class wm_mypost {
    var $post_title;
    var $post_content;
    var $post_status;
    var $post_author;    /* author user id (optional) */
    var $post_name;      /* slug (optional) */
    var $post_type;      /* 'page' or 'post' (optional, defaults to 'post') */
    var $comment_status; /* open or closed for commenting (optional) */
}

// initialize post object
$wm_mypost = new wm_mypost();

// fill object
$wm_mypost->post_title = $title;
$wm_mypost->post_content = $content;
$wm_mypost->post_status = 'private';
$wm_mypost->post_author = 1;

// Optional; uncomment as needed
// $wm_mypost->post_type = 'page';
// $wm_mypost->comment_status = 'closed';

// feed object to wp_insert_post
wp_insert_post($wm_mypost);

This page is marked as incomplete. You can help Codex by expanding it.