WordPress | wp_insert_post Function

Prem Tiwari - - WordPress Functions


wp_insert_post function will insert a post in your database with the minimum required piece of information.

You can set the post date manually, by setting the values for ‘post_date’ and ‘post_date_gmt’ keys. You can close the comments or open the comments by setting the value for ‘comment_status’ key.

Below is the syntax of wp_insert_post() Function, how to use in your WordPress plugin or theme file.

Syntax:

Used Parameters:

$postarr : (array) (Required) An array of elements that make up a post to update or insert.
'ID' : (int) The post ID. If equal to something other than 0, the post with that ID will be updated. Default 0.
'post_author' : (int) The ID of the user who added the post. Default is the current user ID.
'post_date' : (string) The date of the post. Default is the current time.
'post_date_gmt' : (string) The date of the post in the GMT timezone. Default is the value of $post_date.
'post_content' : (mixed) The post content. Default empty.
'post_content_filtered' : (string) The filtered post content. Default empty.
'post_title' : (string) The post title. Default empty.
'post_excerpt' : (string) The post excerpt. Default empty.
'post_status' :(string) The post status. Default ‘draft’.
'post_type' :(string) The post type. Default ‘post’.
'comment_status' :(string) Whether the post can accept comments. Accepts ‘open’ or ‘closed’. Default is the value of ‘default_comment_status’ option.
'ping_status' :(string) Whether the post can accept pings. Accepts ‘open’ or ‘closed’. Default is the value of ‘default_ping_status’ option.
'post_password' :(string) The password to access the post. Default empty.
'post_name' :(string) The post name. Default is the sanitized post title when creating a new post.
'to_ping' :(string) Space or carriage return-separated list of URLs to ping. Default empty.
'pinged': (string) Space or carriage return-separated list of URLs that have been pinged. Default empty.
'post_modified': (string) The date when the post was last modified. Default is the current time.
'post_modified_gmt': (string) The date when the post was last modified in the GMT timezone. Default is the current time.
'post_parent': (int) Set this for the post it belongs to, if any. Default 0.
'menu_order': (int) The order the post should be displayed in. Default 0.
'post_mime_type': (string) The mime type of the post. Default empty.
'guid':(string) Global Unique ID for referencing the post. Default empty.
'post_category':(array) Array of category names, slugs, or IDs. Defaults to value of the ‘default_category’ option.
'tags_input':(array) Array of tag names, slugs, or IDs. Default empty.
'tax_input':(array) Array of taxonomy terms keyed by their taxonomy name. Default empty.
'meta_input':(array) Array of post meta values keyed by their post meta key. Default empty.
$wp_error: (bool) (Optional) Whether to return a WP_Error on failure.

This function is available inside the File: wp-includes/post.php. You can find the function described below:

Example 1:


About: Prem Tiwari

Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies.