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 array as its argument and returns the post ID of the created post (or 0 if there is an error).

wp_insert_post() 描述


 <?php wp_insert_post$post$wp_error ); ?> 

wp_insert_post() 用法



(array) (必填) An array representing the elements that make up a post. There is a one-to-one relationship between these elements and the names of columns in the wp_posts table in the database.

默认值: None


  • post_title and post_content are required
  • post_status: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post. See also Post Status Transitions.
  • post_category: Equivalent to calling wp_set_post_categories().
  • tags_input: Equivalent to calling wp_set_post_tags().
  • tax_input: Equivalent to calling wp_set_post_terms() for each custom taxonomy in the array. If the current user doesn't have the capability to work with a taxonomy, then you must use wp_set_object_terms() instead.
  • page_template: If post_type is 'page', will attempt to set the page template. On failure, the function will return either a WP_Error or 0, and stop before the final actions are called. If the post_type is not 'page', the parameter is ignored. You can set the page template for a non-page by calling update_post_meta() with a key of '_wp_page_template'.


(bool) (可选) Allow return of WP_Error object on failure

默认值: false

wp_insert_post() 参数


  • 添加于 版本: 1.0

wp_insert_post() 历史


wp_insert_post() 函数的代码位于 wp-includes/post.php.

wp_insert_post() 源文件