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

描述

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

使用方法

  1. <?phpxa0wp_insert_post(xa0$post,xa0$wp_errorxa0);xa0?>

参数

参数一:$post

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

参数二:$wp_error

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

返回的值

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

使用例子

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

安全

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

源码位置

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

 

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

评论

   0   访客  0
  • 波浪
  • 波浪
  • 波浪
  • 波浪