WordPress函数-代码插入文章函数-wp_insert_post()

描述

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

使用方法

  1. <?php wp_insert_post( $post$wp_error ); ?>

参数

参数一:$post

  • (数组) (必需) 一个文章对象. 与数据库wp_posts表中的字段对应
  • 默认: 无
  • 重要: 如果设置$post['ID']的值,将不会创建 这个ID的文章. 设置这个值将会更新这个ID的文章. 简单的说,创建一个文章 $post['ID'] 必须为空或不设置这个值.
  1. $post = array(
  2. 'ID' => [ <post id> ] //需要更新的文章编号
  3. 'menu_order' => [ <order> ] //如果新文章是页面,设置显示顺序
  4. 'comment_status' => [ 'closed' | 'open' ] // 评论的状态,'closed'关闭评论.
  5. 'ping_status' => [ 'closed' | 'open' ] // ping的状态,'closed' 关闭 pingbacks和trackbacks
  6. 'pinged' => [ ? ] //该文章被ping到的地址
  7. 'post_author' => [ <user ID> ] //作者编号
  8. 'post_category' => [ array(<category id>, <...>) ] //文章归类数组
  9. 'post_content' => [ <the text of the post> ] //文章内容,必填
  10. 'post_date' => [ Y-m-d H:i:s ] //文章编辑日期
  11. 'post_date_gmt' => [ Y-m-d H:i:s ] //文章编辑GMT日期
  12. 'post_excerpt' => [ <an excerpt> ] //摘要信息
  13. 'post_name' => [ <the name> ] // (slug) 文章别名
  14. 'post_parent' => [ <post ID> ] //新文章的父文章编号
  15. 'post_password' => [ ? ] //文章浏览密码
  16. 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新文章的状态
  17. 'post_title' => [ <the title> ] //文章标题,必填
  18. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //文章类型:文章、页面、链接、菜单、其他定制类型
  19. 'tags_input' => [ '<tag>, <tag>, <...>' ] //标签字符串
  20. 'to_ping' => [ ? ] //该文章需要ping到的地址
  21. 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // 附加注释数组
  22. );

参数二:$wp_error

  • (布尔型) (可选) 失败时是否返回WP_Error对象
  • 默认: false

返回的值

  • 若添加文章成功,返回文章ID。否则返回0.

使用例子

  1. // 创建一个文章对象
  2. $my_post = array(
  3.     'post_title' => '文章标题',
  4.     'post_content' => '文章内容',
  5.     'post_status' => 'publish',
  6.     'post_author' => 1,
  7.     'post_category' => array(8,39)
  8. );
  9. //插入  
  10. wp_insert_post( $my_post );

安全

函数会自动过滤和检查文章信息的合法性,不需要用户自己来额外处理

源码位置

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

 

喜欢就支持以下吧
倾尘SEO
  • 本文由 倾尘SEO 发表于 2018年7月11日
  • 转载请务必保留本文链接:https://www.qcwlseo.com/hs-wp_insert_post.html
 评论   0   访客   0