wp insert post 插入文章到数据库-做投稿或者前端表单时使用

函数描述

此函数的作用是插入文章(或页面、自定义文章类型)到数据库,插入之前,会净化一些变量,做一些检查,补全一些缺失的必需数据(如日期/时间)。此函数需要一个数组作为参数,插入成功后,返回插入文章的 ID(插入失败返回0)。

使用方法

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

参数

$post
(array) (必需) 代表一篇文章数组,数组元素和 wp_posts 数据表中的数据列是一对一关系。
默认: None

重要:为 $post[‘ID’] 设置一个值将不会创建 ID 为该值的文章,而是更新 ID 为该值的文章,也就是说,要想插入一篇新文章,$post[‘ID’] 必须为空,或者压根不设置。

文章数组的内容取决于你对文章默认值的信任程度,下面是所有文章数组元素的简短描述。

$postarr = array(
    'ID'                    => '0', // (int)默认值为0,如果是0以外的值,则该 ID 的文章将被更新。
    'post_author'           => get_current_user_id(), // (int)文章作者的ID,默认为当前登录的用户ID
    'post_date'             => '', // 文章发布时间,默认为当前时间。
    'post_date_gmt'         => '', // (字符串)GMT格式的文章发布时间。默认值是$post_date的值。
    'post_content'          => '', // (字符串)文章内容,默认为空。
    'post_content_filtered' => '', // (字符串)过滤后的内容,默认是空的。不要管这个,WordPress会自动处理。
    'post_title'            => '', // (字符串)文章标题,默认为空.
    'post_excerpt'          => '', // (字符串)文章摘要,默认为空。
    'post_status'           => '', // (字符串)文章状态,默认为『draft』,即草稿。
    'post_type'             => '', // (字符串)文章类型,默认为『post』.
    'comment_status'        => '', // (字符串)是否可以接受评论。接受『打开』或『关闭』。默认值是『default_comment_status』选项的值。
    'ping_status'           => '', // (字符串)是否可以接受ping命令。接受『打开』或『关闭』。默认值是『default_ping_status』选项的值。
    'post_password'         => '', // (字符串)访问该文章的密码,默认是空的。
    'post_name'             => '', // (字符串)文章的别名,当发布新的文章时会默认创建。
    'to_ping'               => '', // (字符串)空格或回车将url的列表分隔成ping,默认是空的。
    'pinged'                => '', // (字符串)空格或回车分隔的url列表,默认是空的。
    'post_modified'         => '', // (字符串)上次修改后的日期,默认是当前时间。
    'post_modified_gmt'     => '', // (字符串)最后在GMT时区修改后的日期,默认是当前时间。
    'post_parent'           => '', // (int)文章的父级文章ID,默认为 0。
    'menu_order'            => '', // (int)如果新文章为一个页面,可以设置一个页面序号,默认为 0。
    'post_mime_type'        => '', // (字符串)文章的mime类型,默认是空的。
    'guid'                  => '', // (字符串)全局唯一ID,用于引用post,默认是空的。
    'post_category'         => '', // (数组)文章分类目录,默认值为『default_category』选项的值。
    'tags_input'            => '', // (数组)文章标签,默认为空。
    'tax_input'             => '', // (数组)文章的自定义分类法项目,默认为空。
    'meta_input'            => '', // (数组)自定义字段,默认为空。
    'page_template'         => '', // 页面模板文件的名称,如,template.php,默认为空。
);

注意

  • post_name, post_title, post_content, 和 post_excerpt 为必需的元素。
  • ‘post_status’:如果设置了 post_status 为 ‘future’,你还必须指定 post_date 值,这样 WordPress 才能知道什么时候发布你的文章,更多信息参见 文章状态转换。
  • ‘post_category’:等效于调用 wp_set_post_categories()。
  • ‘tags_input’:等效于调用 wp_set_post_tags()。
  • ‘tax_input:等效于为数组中的每个自定义分类法调用 wp_set_post_terms(),如果当前用户没有设置自定义分类法的权限,就必须使用 wp_set_object_terms() 代替了。
  • ‘page_template’:如果 post_type 为 ‘page’,将尝试设置页面,如果设置失败,此函数将返回一个 WP_Error 对象或 0,然后在最终操作之前停止。如果 post_type 不是 ‘page’,此参数将被忽略,你可以通过调用 update_post_meta() 设置 ‘_wp_page_template’ 的值为不是页面的文章类型设置页面模板。

$wp_error
(bool) (optional) 失败时允许返回一个 WP_Error 对象
默认: false

返回值

如果文章成功插入了数据库,将返回插入的新文章 ID,如果失败,将返回 0 或一个 WP_Error 对象(如果 $wp_error 设置为 true )。

使用示例

在调用 wp_insert_post() 之前,我们需要创建一个包含必要文章元素的数组,wp_insert_post() 将会使用默认值自动填充一些文章元素,但是,用户必须提供一个文章标题和内容,否则,将会出现数据库错误导致插入文章失败。

下面的例子中,我么设置了 post title, content, status, author, 和 post categories,除了这些,我们可以根据上面的列表添加更多的文章元素键值对,以匹配 wp_posts 数据表中的数据列。

// 创建文章对象
$my_post = array(
'post_title' => '我的测试文章',
'post_content' => '这是一个测试文章。',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);

// 插入文章到数据库
wp_insert_post( $my_post );

文章插入成功后,将返回新文章 ID。

$post_id = wp_insert_post( $post, $wp_error );
//现在,我们可以使用 $post_id 来 add_post_meta 或 update_post_meta

上面提到的文章元素默认值为下面数组:

$defaults = array(
'post_status' => 'draft',
'post_type' => 'post',
'post_author' => $user_ID,
'ping_status' => get_option('default_ping_status'),
'post_parent' => 0,
'menu_order' => 0,
'to_ping' => '',
'pinged' => '',
'post_password' => '',
'guid' => '',
'post_content_filtered' => '',
'post_excerpt' => '',
'import_id' => 0
);

分类目录

分类目录应该以分类 ID 数组的形式传入,即使只需要设置一个分类目录,该参数的值也必须为数组。

更多信息参见:wp_set_post_terms()

安全问题

在存入数据库之前,wp_insert_post() 先把数据传递给 sanitize_post() 处理了,也就是说,该函数已经处理了所有的数据验证和净化,我们不需要再为这些问题操心了。

因为一些原因,你可能需要移除文章标题或其他字段中的 HTML, JavaScript, 和 PHP 代码,奇怪的是, WordPress 竟然没有自动处理这些事情,不过我们可以使用 wp_strip_all_tags() 函数(WordPress 2.9 以后可用)轻松的搞定,这在提交前端表单的时候,特别有用。

// 创建文章对象、文章投稿
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);

// 插入文章到数据库
wp_insert_post( $my_post );

来自:https://www.wpzhiku.com/codex/wp_insert_post/