描述
該函數用于更新wordpress數據庫中的文章。如希望函數正常運行,必須傳遞將被更新的文章編號ID。
使用方法
- <?php?wp_update_post(?$post?);??>
例子
調用wp_update_post( )前需創建一個數組以傳遞必要元素。與 wp_insert_post()不同的是,這里只需要傳遞將更新的文章編號和元素。元素名稱應與數據庫中名稱相匹配。
- ??$my_post?=?array();
- ??$my_post['ID']?=?37;
- ??$my_post['post_content']?=?'This?is?the?updated?content.';
- ??wp_update_post(?$my_post?);
類別
需要將類別作為整數數組傳遞,該數組應與數據庫中的類別編號相匹配。即使文章只屬于某一項類別,情況也應如此。
參數
$post
(數組)(可選)能表示可組成文章元素的對象。這些元素與數據庫wp_posts表格中的縱列名稱應一一對應??梢圆惶畛銲D(編號)字段,這樣的話使用該函數幾乎沒有任何意義。
默認值:一個空數組
返回的值
若文章成功加入數據庫,返回文章編號。否則返回0.
相關函數
源文件
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);
- }