说明
() 函数用于更新数据库中的文章。
为了保证功能正常运行wordpress调用参数,更新后的文章ID必须传入
如何使用
例子
在调用 ( ) 之前创建一个数组以传递必要的元素。与 () 不同的是wordpress调用参数,这里只需要传递将要更新的文章编号和元素。元素名称应与数据库中的名称匹配。
// 更新编号为37的文章
$my_post = array();
$my_post['ID'] = 37;
$my_post['post_content'] = 'This is the updated content.';
// Update the post into the database
wp_update_post( $my_post );

类别
类别需要作为整数数组传递,该数组应与数据库中的类别编号匹配。即使文章只属于一个类别wordpress网站建设,情况也应该如此。
函数参数
$post (array) (可选) 可以表示可以构成 post 元素的对象。这些元素应该与数据库表中的列名一一对应。 ID(编号)字段可以不填写,使用此功能意义不大。
默认:一个空数组
返回值
如果文章成功添加到数据库wordpress做网站,则返回文章编号。否则返回 0.
源文件
() 在 wp-/post.php.
/**
* Update a post with new post data.

*
* The date does not have to be set for drafts. You can set the date and it will
* not be overridden.
*
* @since 1.0.0
*
* @param array|object $postarr Post data. Arrays are expected to be escaped, objects are not.
* @return int 0 on failure, Post ID on success.
*/
function wp_update_post($postarr = array()) {
if ( is_object($postarr) ) {
// non-escaped post was passed

$postarr = get_object_vars($postarr);
$postarr = add_magic_quotes($postarr);
}
// First, get all of the original fields
$post = wp_get_single_post($postarr['ID'], ARRAY_A);
// Escape data pulled from DB.
$post = add_magic_quotes($post);
// Passed post category list overwrites existing category list if not empty.
if ( isset($postarr['post_category']) && is_array($postarr['post_category'])

&& 0 != count($postarr['post_category']) )
$post_cats = $postarr['post_category'];
else
$post_cats = $post['post_category'];
// Drafts shouldn't be assigned a date unless explicitly done so by the user
if ( isset( $post['post_status'] )
&& in_array($post['post_status'], array('draft', 'pending', 'auto-draft'))
&& empty($postarr['edit_date'])
&& ('0000-00-00 00:00:00' == $post['post_date_gmt']) )
$clear_date = true;
else

$clear_date = false;
// Merge old and new fields with new fields overwriting old ones.
$postarr = array_merge($post, $postarr);
$postarr['post_category'] = $post_cats;
if ( $clear_date ) {
$postarr['post_date'] = current_time('mysql');
$postarr['post_date_gmt'] = '';
}
if ($postarr['post_type'] == 'attachment')
return wp_insert_attachment($postarr);
return wp_insert_post($postarr);
}
文章来自互联网,侵权请联系删除,文章阐述观点来自文章出处,并不代表本站观点。
www.8001717.cn