wp_insert_post()使用代码插入一篇文章

目录

描述

译文

该函数可在数据库中插入文章(及页面)。它可以进行处理变量,检查操作,填充日期/时间等缺失变量等工作。该函数以对象作为变量,返回已创建文章的编号(出错时返回0)。

原文

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() 用法

参数

$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

Notes

  • 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'.

$wp_error

(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() 源文件

相关