WordPress:Function Reference/wp insert post
查看全部产品文档命名空间:WordPress
描述
这个函数向数据库插入文章(和网页),清理了一些变数,做一些检查,填入缺失的变数,如时间/日期等等。这个函数将一个对象作为变数,并且返回创建的文章的ID(如果有错误的话,返回是0)。
用法
%%% <?php wp_insert_post( $post ); ?> %%%
例子
调用wp_insert_post()之前,有必要创建一个对象(通常是一个数组),来传递组成文章的必要成分。wp_insert_post()会找出这些的默认列表,但是用户需要提供标题和内容,否则数据库写操作就会被破坏。
除了这儿定义的数据库中的新的关键字之外,用户还可以提供更多的内容。关键字应该与数据库中的wp_posts表格中的栏名称相匹配。
// 创建文章object $my_post = array(); $my_post['post_title'] = 'My post'; $my_post['post_content'] = 'This is my post.'; $my_post['post_status'] = 'publish'; $my_post['post_author'] = 1; // 将文章插入到数据库中 wp_insert_post( $my_post );
关于上面内容的默认列表定义在函数主体中,如下:
$defaults = array(
'post_status' => 'draft',
'post_type' => 'post',
'post_author' => $user_ID,
'ping_status' => get_option('default_ping_status'),
'post_parent' => 0,
'menu_order' => 0,
'to_ping' => '',
'pinged' => '',
'post_password' => '',
'guid' => '',
'post_content_filtered' => '',
'post_excerpt' => ''
);
参数
返回
如果文章成功添加到数据库,会返回文章的ID。否则会返回0。
相关的
wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment()