WordPress插入文章函数:wp_insert_post

2022年5月18日 飞飞 阅读(760)
知识付费主题,优惠促销中:xx-blog主题

WordPress函数wp_insert_post用于插入一篇新的文章。

wp_insert_post( array $postarr, bool $wp_error = false, bool $fire_after_hooks = true )

函数参数

$postarr

数组

ID:文章的ID,如果传递0以外的值,则更新相应ID的文章而不是插入新的文章;
post_author:作者的ID,默认为当前作者;
post_date:发表时间,格式为Y-m-d H:i:s,默认为当前时间;
post_date_gmt:发表文章的格林时间,与国内时区相差8小时;
post_content:文章内容;
post_content_filtered:文章内容过滤;
post_title:文章标题;
post_excerpt:文章摘要;
post_status:文章状态,默认值:draft
post_type:文章类型,默认值:post
comment_status:是否允许评论,可用值:open或closed,默认取wp_options表中default_comment_status的值,即后台讨论设置中“允许他人在新文章上发表评论”选项;
ping_status:是否允许其他博客发送链接通知,可用值:open或closed,默认取wp_options表中default_ping_status的值,即后台讨论设置中“允许其他博客发送链接通知(pingback和trackback)到新文章”选项;
post_password:访问密码,默认为空;
post_name:文章别名;
to_ping:使用空格或回车分隔要ping的URL列表;
pinged:空格或回车分隔已ping的url列表;
post_modified:文章的修改时间,格式为Y-m-d H:i:s,默认为当前时间;
post_modified_gmt:修改文章的格林时间;
post_parent:父级ID;
menu_order:文章的显示顺序;
post_mime_type:MIME类型,适用于插入附件时;
guid:文章链接,当设置固定链接后为伪静态格式,默认为动态链接;
post_category:数组,文章分类的ID;
tags_input:数组,标签名称、别名或ID;
tax_input:数组,自定义分类法名称;
meta_input:数组,post meta的键值对;
$wp_error

布尔值,默认值:false

是否返回错误信息

$fire_after_hooks

布尔值,默认值:true

是否启用插入文章后的hooks

函数使用示例

$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 );

扩展阅读

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


关注微信公众号『xx主题网

第一时间了解最新网络动态
关注博主不迷路~

未经允许不得转载:xx主题网 » WordPress插入文章函数:wp_insert_post
分享到:

评论抢沙发

要发表评论,您必须先

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

扫码关注微信公众号

扫描关注xx主题网