描述
wp_insert_post()函数可在数据库中插入文章(及页面)。它可以进行处理变量,检查操作,填充日期/时间等缺失变量等工作。该函数以对象作为变量,返回已创建文章的编号(出错时返回0)。
使用方法
- <?phpxa0wp_insert_post(xa0$post,xa0$wp_errorxa0);xa0?>
参数
参数一:$post
- (数组) (必需) 一个文章对象. 与数据库wp_posts表中的字段对应
- 默认: 无
- 重要: 如果设置$post['ID']的值,将不会创建 这个ID的文章. 设置这个值将会更新这个ID的文章. 简单的说,创建一个文章 $post['ID'] 必须为空或不设置这个值.
- $postxa0=xa0array(
- 'ID'xa0=>xa0[xa0<postxa0id>xa0]xa0//需要更新的文章编号
- 'menu_order'xa0=>xa0[xa0<order>xa0]xa0//如果新文章是页面,设置显示顺序
- 'comment_status'xa0=>xa0[xa0'closed'xa0|xa0'open'xa0]xa0//xa0评论的状态,'closed'关闭评论.
- 'ping_status'xa0=>xa0[xa0'closed'xa0|xa0'open'xa0]xa0//xa0ping的状态,'closed'xa0关闭xa0pingbacks和trackbacks
- 'pinged'xa0=>xa0[xa0?xa0]xa0//该文章被ping到的地址
- 'post_author'xa0=>xa0[xa0<userxa0ID>xa0]xa0//作者编号
- 'post_category'xa0=>xa0[xa0array(<categoryxa0id>,xa0<...>)xa0]xa0//文章归类数组
- 'post_content'xa0=>xa0[xa0<thexa0textxa0ofxa0thexa0post>xa0]xa0//文章内容,必填
- 'post_date'xa0=>xa0[xa0Y-m-dxa0H:i:sxa0]xa0//文章编辑日期
- 'post_date_gmt'xa0=>xa0[xa0Y-m-dxa0H:i:sxa0]xa0//文章编辑GMT日期
- 'post_excerpt'xa0=>xa0[xa0<anxa0excerpt>xa0]xa0//摘要信息
- 'post_name'xa0=>xa0[xa0<thexa0name>xa0]xa0//xa0(slug)xa0文章别名
- 'post_parent'xa0=>xa0[xa0<postxa0ID>xa0]xa0//新文章的父文章编号
- 'post_password'xa0=>xa0[xa0?xa0]xa0//文章浏览密码
- 'post_status'xa0=>xa0[xa0'draft'xa0|xa0'publish'xa0|xa0'pending'|xa0'future'xa0|xa0'private'xa0]xa0//新文章的状态
- 'post_title'xa0=>xa0[xa0<thexa0title>xa0]xa0//文章标题,必填
- 'post_type'xa0=>xa0[xa0'post'xa0|xa0'page'xa0|xa0'link'xa0|xa0'nav_menu_item'xa0|xa0customxa0postxa0typexa0]xa0//文章类型:文章、页面、链接、菜单、其他定制类型
- 'tags_input'xa0=>xa0[xa0'<tag>,xa0<tag>,xa0<...>'xa0]xa0//标签字符串
- 'to_ping'xa0=>xa0[xa0?xa0]xa0//该文章需要ping到的地址
- 'tax_input'xa0=>xa0[xa0array(xa0'taxonomy_name'xa0=>xa0array(xa0'term',xa0'term2',xa0'term3'xa0)xa0)xa0]xa0//xa0附加注释数组
- );
参数二:$wp_error
- (布尔型) (可选) 失败时是否返回WP_Error对象
- 默认: false
返回的值
- 若添加文章成功,返回文章ID。否则返回0.
使用例子
- //xa0创建一个文章对象
- $my_postxa0=xa0array(
- xa0xa0xa0xa0'post_title'xa0=>xa0'文章标题',
- xa0xa0xa0xa0'post_content'xa0=>xa0'文章内容',
- xa0xa0xa0xa0'post_status'xa0=>xa0'publish',
- xa0xa0xa0xa0'post_author'xa0=>xa01,
- xa0xa0xa0xa0'post_category'xa0=>xa0array(8,39)
- );
- //插入xa0xa0
- wp_insert_post(xa0$my_postxa0);
安全
函数会自动过滤和检查文章信息的合法性,不需要用户自己来额外处理
源码位置
wp_insert_post() 函数位于 wp-includes/post.php
评论
0 访客 0