wp_insert_post() 插入文章到数据库,WordPress 制作投稿页

 4年前     1.9K  

文章目录

函数描述

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

使用方法

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

参数

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

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

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

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

$post = array(
  'ID'             => [ <post id> ] // 如果需要更新文章,设置id为需要更新文章的id,否则不要设置此值
  'post_content'   => [ <string> ] // 文章内容,也就是可视化编辑器里面的输入的内容
  'post_name'      => [ <string> ] // 文章的别名,就是URL里面的名称
  'post_title'     => [ <string> ] // 文章标题
  'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // 文章状态,默认 'draft'.
  'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // 文章类型,默认为'post'.
  'post_author'    => [ <user ID> ] // 文章作者的ID,默认为当前登录的用户ID
  'ping_status'    => [ 'closed' | 'open' ] // 是否允许 Pingbacks 或 trackbacks allowed,默认为'default_ping_status' 设置的值。
  'post_parent'    => [ <post ID> ] // 文章的父级文章ID,默认为 0,顶级文章。
  'menu_order'     => [ <order> ] // 如果新文章为一个页面,可以设置一个页面序号,默认为0。
  'to_ping'        => // 空格或回车分隔的需要ping的url列表,默认为空字符串。
  'pinged'         => // 空格或回车分隔的已经ping过的url列表,默认为空字符串。
  'post_password'  => [ <string> ] // 文章密码,默认为空字符串。
  'guid'           => // 不要管这个,WordPress会自动处理。
  'post_content_filtered' => // 不要管这个,WordPress会自动处理。
  'post_excerpt'   => [ <string> ] // 文章摘要。
  'post_date'      => [ Y-m-d H:i:s ] // 文章发布时间。
  'post_date_gmt'  => [ Y-m-d H:i:s ] // GMT格式的文章发布时间。
  'comment_status' => [ 'closed' | 'open' ] // 是否允许评论,默认为 'default_comment_status'的值,或'closed'。
  'post_category'  => [ array(<category id>, ...) ] // 文章分类目录,默认为空
  'tags_input'     => [ '<tag>, <tag>, ...' | array ] // 文章标签,默认为空
  'tax_input'      => [ array( <taxonomy> => <array | string>, <taxonomy_other> => <array | string> ) ] // 文章的自定义分类法项目,默认为空。
  'page_template'  => [ <string> ] // 页面模板文件的名称,如,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_post_terms() 代替了。
  • page_template:如果 post_type 为 ‘page’,将尝试设置页面,如果设置失败,此函数将返回一个 WP_Error 对象或 0,然后在最终操作之前停止。如果 post_type 不是 ‘page’,此参数将被忽略,你可以通过调用 update_post_meta() 设置 ‘_wp_page_template’ 的值为不是页面的文章类型设置页面模板。

返回值

如果文章成功插入了数据库,将返回插入的新文章 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 );

 

版权声明:一为 发表于 4年前,共 3661 字。
转载请注明:wp_insert_post() 插入文章到数据库,WordPress 制作投稿页 | 一为忆

您可能感兴趣的

暂无评论

暂无评论...