描述
該函數(shù)用于更新wordpress數(shù)據(jù)庫中的文章。如希望函數(shù)正常運(yùn)行,必須傳遞將被更新的文章編號(hào)ID。
使用方法
- <?php?wp_update_post(?$post?);??>
例子
調(diào)用wp_update_post( )前需創(chuàng)建一個(gè)數(shù)組以傳遞必要元素。與 wp_insert_post()不同的是,這里只需要傳遞將更新的文章編號(hào)和元素。元素名稱應(yīng)與數(shù)據(jù)庫中名稱相匹配。
- ??$my_post?=?array();
- ??$my_post['ID']?=?37;
- ??$my_post['post_content']?=?'This?is?the?updated?content.';
- ??wp_update_post(?$my_post?);
類別
需要將類別作為整數(shù)數(shù)組傳遞,該數(shù)組應(yīng)與數(shù)據(jù)庫中的類別編號(hào)相匹配。即使文章只屬于某一項(xiàng)類別,情況也應(yīng)如此。
參數(shù)
$post
(數(shù)組)(可選)能表示可組成文章元素的對(duì)象。這些元素與數(shù)據(jù)庫wp_posts表格中的縱列名稱應(yīng)一一對(duì)應(yīng)。可以不填充ID(編號(hào))字段,這樣的話使用該函數(shù)幾乎沒有任何意義。
默認(rèn)值:一個(gè)空數(shù)組
返回的值
若文章成功加入數(shù)據(jù)庫,返回文章編號(hào)。否則返回0.
相關(guān)函數(shù)
源文件
wp_update_post() 位于 wp-includes/post.php.
- function?wp_update_post($postarr?=?array())?{
- ?if?(?is_object($postarr)?)?{
- ??
- ??$postarr?=?get_object_vars($postarr);
- ??$postarr?=?add_magic_quotes($postarr);
- ?}
- ?
- ?$post?=?wp_get_single_post($postarr['ID'],?ARRAY_A);
- ?
- ?$post?=?add_magic_quotes($post);
- ?
- ?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'];
- ?
- ?if?(?isset(?$post['post_status']?)?&&?in_array($post['post_status'],?array('draft',?'pending',?'auto-draft'))?&&?emptyempty($postarr['edit_date'])?&&
- ????('0000-00-00?00:00:00'?==?$post['post_date_gmt'])?)
- ??$clear_date?=?true;
- ?else
- ??$clear_date?=?false;
- ?
- ?$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);
- }