微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress插入或更新文章函数wp_insert_post()

在做wordpress主题定制开发或插件开发的时候,经常需要通过代码插入或者更新一篇文章,这个时候我们就可以使用wordpress自带文章插入函数wp_insert_post();

函数基本用法

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

函数参数详解

$post

要插入或更新的文章对象,和数据库中的wp_posts数据表中的字段一一对应;

$wp_error

认:false

文章插入或更新失败时是否返回WP_Error对象

其中$post对象具体参数如下:

$post = array(

'ID' => [ <post id> ] //需要更新的文章编号,如果不设置,则为插入一篇新文章

'menu_order' => [ <order> ] //如果新文章页面,设置显示顺序

'comment_status' => [ 'closed' | 'open' ] // 评论的状态,'closed'关闭评论.

'ping_status' => [ 'closed' | 'open' ] // ping的状态,'closed' 关闭 pingbacks和trackbacks

'pinged' => [ ? ] //该文章被ping到的地址

'post_author' => [ <user ID> ] //作者编号

'post_category' => [ array(<category id>,<...>) ] //文章归类数组

'post_content' => [ <the text of the post> ] //文章内容,必填

'post_date' => [ Y-m-d H:i:s ] //文章编辑日期

'post_date_gmt' => [ Y-m-d H:i:s ] //文章编辑GMT日期

'post_excerpt' => [ <an excerpt> ] //摘要信息

'post_name' => [ <the name> ] // (slug) 文章别名

'post_parent' => [ <post ID> ] //新文章的父文章编号

'post_password' => [ ? ] //文章浏览密码

'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新文章的状态

'post_title' => [ <the title> ] //文章标题,必填

'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //文章类型文章页面链接菜单、其他定制类型

'tags_input' => [ '<tag>,<tag>,<...>' ] //标签字符串

'to_ping' => [ ? ] //该文章需要ping到的地址

'tax_input' => [ array( 'taxonomy_name' => array( 'term','term2','term3' ) ) ] // 附加注释数组

);

函数返回值

如果文章成功插入或更新,返回文章编号。否则返回0.

函数使用方法举例

$my_post = array(

'post_title' => 'My post',

'post_content' => 'This is my post.',

'post_status' => 'publish',

'post_author' => 1,

'post_category' => array(8,39)

);

wp_insert_post( $my_post );

以上就是本文关于wordpress插入或更新文章函数wp_insert_post()的详细介绍和使用方法举例,希望能对您有所帮助。

原文地址:https://www.jb51.cc/wordpress/4741930.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐