Admin wordpress tips
wp_insert_post( $post, $wp_error );

This function inserts posts,pages or your custom post type in the database.

This function's first parameter is $post, which type is array. You must set this parameter. This array has many keys, merely you can omit to put in default values. Keys are listed below :

'ID' => If you set this value, that doesn't mean it will create a post with that ID number. But it will update the post with that ID number with the other values specified in this array.
So if you want to insert a new post, this key must be blank or not set.

'post_content' => The content of the post

'post_name' => The name(slug) for your post

'post_title' => The title of your post.

'post_status' => Set this key to your need. Default value is 'Draft'

'post_type' => This key set the value for your post. Default value is 'post'

'post_author' => The user ID number of the author. Default is the current user ID.

'post_parent' => Sets the parent of the new post if any. Default 0.

This keys are main keys.
Other keys which rarely used are : 'menu_order', 'to_ping', 'pinged', 'post_password', 'guid', 'post_content_filtered', 'post_excerpt', 'post_date', 'post_date_gmt', 'comment_status', 'post_category', 'tags_input', 'tax_input', 'page_template'.

Some important notes which you have to remember are :

1. If you set a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post.

This function's second parameter is $wp_error. This is optional parameter. This will allow return of WP_Error object on failure if it is set to true.

Example :

// Create post object
$example_post = array(
'post_title' => 'Test post',
'post_content' => 'This is test post.',
'post_status' => 'publish',
'post_author' => 1
);

// Insert the post into the database
wp_insert_post( $example_post );
Like · Comment ·
Ansari Md Tahir and 2 other like this
Download Android App