女武神的终末第三季免费观看,国产精品va无码二区,欧美日韩在线视频观看,国产在线观看网站

WordPress更新文章函數:wp_update_post

2016-10-16 wordpress函數
  • 文章介紹
  • 快速入門
  • 評價&建議

描述

該函數用于更新wordpress數據庫中的文章。如希望函數正常運行,必須傳遞將被更新的文章編號ID。

使用方法

  1. <?php?wp_update_post(?$post?);??>

例子

調用wp_update_post( )前需創建一個數組以傳遞必要元素。與 wp_insert_post()不同的是,這里只需要傳遞將更新的文章編號和元素。元素名稱應與數據庫中名稱相匹配。

  1. //?更新編號為37的文章
  2. ??$my_post?=?array();
  3. ??$my_post['ID']?=?37;
  4. ??$my_post['post_content']?=?'This?is?the?updated?content.';
  5. //?Update?the?post?into?the?database
  6. ??wp_update_post(?$my_post?);

類別

需要將類別作為整數數組傳遞,該數組應與數據庫中的類別編號相匹配。即使文章只屬于某一項類別,情況也應如此。

參數

$post

(數組)(可選)能表示可組成文章元素的對象。這些元素與數據庫wp_posts表格中的縱列名稱應一一對應??梢圆惶畛銲D(編號)字段,這樣的話使用該函數幾乎沒有任何意義。

默認值:一個空數組

返回的值

若文章成功加入數據庫,返回文章編號。否則返回0.

相關函數

  1. wp_insert_post()

源文件

wp_update_post() 位于 wp-includes/post.php.

  1. /**
  2. ?*?Update?a?post?with?new?post?data.
  3. ?*
  4. ?*?The?date?does?not?have?to?be?set?for?drafts.?You?can?set?the?date?and?it?will
  5. ?*?not?be?overridden.
  6. ?*
  7. ?*?@since?1.0.0
  8. ?*
  9. ?*?@param?array|object?$postarr?Post?data.?Arrays?are?expected?to?be?escaped,?objects?are?not.
  10. ?*?@return?int?0?on?failure,?Post?ID?on?success.
  11. ?*/
  12. function?wp_update_post($postarr?=?array())?{
  13. ?if?(?is_object($postarr)?)?{
  14. ??//?non-escaped?post?was?passed
  15. ??$postarr?=?get_object_vars($postarr);
  16. ??$postarr?=?add_magic_quotes($postarr);
  17. ?}
  18. ?//?First,?get?all?of?the?original?fields
  19. ?$post?=?wp_get_single_post($postarr['ID'],?ARRAY_A);
  20. ?//?Escape?data?pulled?from?DB.
  21. ?$post?=?add_magic_quotes($post);
  22. ?//?Passed?post?category?list?overwrites?existing?category?list?if?not?empty.
  23. ?if?(?isset($postarr['post_category'])?&&?is_array($postarr['post_category'])
  24. ????&&?0?!=?count($postarr['post_category'])?)
  25. ??$post_cats?=?$postarr['post_category'];
  26. ?else
  27. ??$post_cats?=?$post['post_category'];
  28. ?//?Drafts?shouldn't?be?assigned?a?date?unless?explicitly?done?so?by?the?user
  29. ?if?(?isset(?$post['post_status']?)?&&?in_array($post['post_status'],?array('draft',?'pending',?'auto-draft'))?&&?emptyempty($postarr['edit_date'])?&&
  30. ????('0000-00-00?00:00:00'?==?$post['post_date_gmt'])?)
  31. ??$clear_date?=?true;
  32. ?else
  33. ??$clear_date?=?false;
  34. ?//?Merge?old?and?new?fields?with?new?fields?overwriting?old?ones.
  35. ?$postarr?=?array_merge($post,?$postarr);
  36. ?$postarr['post_category']?=?$post_cats;
  37. ?if?(?$clear_date?)?{
  38. ??$postarr['post_date']?=?current_time('mysql');
  39. ??$postarr['post_date_gmt']?=?'';
  40. ?}
  41. ?if?($postarr['post_type']?==?'attachment')
  42. ??return?wp_insert_attachment($postarr);
  43. ?return?wp_insert_post($postarr);
  44. }
5 0

企業建站推薦正版商業主題,國內專業團隊開發,完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計幫助1300+客戶成功建站,為站長提供支持!

立刻開啟你的建站之旅
QQ在線客服

服務熱線

wordpress建站咨詢