久青草国产观看在线视频,在线观看欧美日女,777毛片,亚洲国产精品99久久久久久久

WordPress文章插入函數(shù):wp_insert_post

2016-10-16 wordpress函數(shù)
  • 文章介紹
  • 快速入門
  • 評(píng)價(jià)&建議

描述

該函數(shù)可在wordpress數(shù)據(jù)庫(kù)中插入文章(及頁(yè)面)。它可以進(jìn)行處理變量,檢查操作,填充日期/時(shí)間等缺失變量等工作。該函數(shù)以對(duì)象作為變量,返回已創(chuàng)建文章的編號(hào)(出錯(cuò)時(shí)返回0)。

使用方法

  1. <?php?wp_insert_post(?$post,?$wp_error?);??>

參數(shù)

$post

(array) (必需) 一個(gè)文章對(duì)象. 與數(shù)據(jù)庫(kù)wp_posts表中的字段一一對(duì)應(yīng)

默認(rèn): 無(wú)

重要: 如果設(shè)置$post[‘ID’]的值,將不會(huì)創(chuàng)建 這個(gè)ID的文章. 設(shè)置這個(gè)值將會(huì)更新這個(gè)ID的文章. 簡(jiǎn)單的說(shuō),創(chuàng)建一個(gè)文章 $post[‘ID’] 必須為空或不設(shè)置這個(gè)值。

  1. $post = array(  
  2.   
  3. 'ID' => [ <post id> ] //需要更新的文章編號(hào)  
  4.   
  5. 'menu_order' => [ <order> ] //如果新文章是頁(yè)面,設(shè)置顯示順序  
  6.   
  7. 'comment_status' => [ 'closed' | 'open' ] // 評(píng)論的狀態(tài),'closed'關(guān)閉評(píng)論.  
  8.   
  9. 'ping_status' => [ 'closed' | 'open' ] // ping的狀態(tài),'closed' 關(guān)閉 pingbacks和trackbacks  
  10.   
  11. 'pinged' => [ ? ] //該文章被ping到的地址  
  12.   
  13. 'post_author' => [ <user ID> ] //作者編號(hào)  
  14.   
  15. 'post_category' => [ array(<category id>, <...>) ] //文章歸類數(shù)組  
  16.   
  17. 'post_content' => [ <the text of the post> ] //文章內(nèi)容,必填  
  18.   
  19. 'post_date' => [ Y-m-d H:i:s ] //文章編輯日期  
  20.   
  21. 'post_date_gmt' => [ Y-m-d H:i:s ] //文章編輯GMT日期  
  22.   
  23. 'post_excerpt' => [ <an excerpt> ] //摘要信息  
  24.   
  25. 'post_name' => [ <the name> ] // (slug) 文章別名  
  26.   
  27. 'post_parent' => [ <post ID> ] //新文章的父文章編號(hào)  
  28.   
  29. 'post_password' => [ ? ] //文章瀏覽密碼  
  30.   
  31. 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新文章的狀態(tài)  
  32.   
  33. 'post_title' => [ <the title> ] //文章標(biāo)題,必填  
  34.   
  35. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //文章類型:文章、頁(yè)面、鏈接、菜單、其他定制類型  
  36.   
  37. 'tags_input' => [ '<tag>, <tag>, <...>' ] //標(biāo)簽字符串  
  38.   
  39. 'to_ping' => [ ? ] //該文章需要ping到的地址  
  40.   
  41. 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // 附加注釋數(shù)組  
  42.   
  43. );  

$wp_error

(布爾型) (可選) 失敗時(shí)是否返回WP_Error對(duì)象

默認(rèn): false

返回的值

若文章成功加入數(shù)據(jù)庫(kù),返回文章編號(hào)。否則返回0.

使用方法

  1. <?php?wp_insert_post(?$post,?$wp_error?);??>

例子

// 創(chuàng)建一個(gè)文章對(duì)象

  1. $my_post?=?array(
  2. 'post_title'?=>?'My?post',
  3. 'post_content'?=>?'This?is?my?post.',
  4. 'post_status'?=>?'publish',
  5. 'post_author'?=>?1,
  6. 'post_category'?=>?array(8,39)
  7. );
  8. //入庫(kù)
  9. wp_insert_post(?$my_post?);

安全

函數(shù)會(huì)自動(dòng)過(guò)濾和檢查文章信息的合法性,不需要用戶自己來(lái)額外處理

源碼位置

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

  1. /**
  2. ?*?Insert?a?post.
  3. ?*
  4. ?*?If?the?$postarr?parameter?has?'ID'?set?to?a?value,?then?post?will?be?updated.
  5. ?*
  6. ?*?You?can?set?the?post?date?manually,?but?setting?the?values?for?'post_date'
  7. ?*?and?'post_date_gmt'?keys.?You?can?close?the?comments?or?open?the?comments?by
  8. ?*?setting?the?value?for?'comment_status'?key.
  9. ?*
  10. ?*?The?defaults?for?the?parameter?$postarr?are:
  11. ?*?????'post_status'???–?Default?is?'draft'.
  12. ?*?????'post_type'?????–?Default?is?'post'.
  13. ?*?????'post_author'???–?Default?is?current?user?ID?($user_ID).?The?ID?of?the?user?who?added?the?post.
  14. ?*?????'ping_status'???–?Default?is?the?value?in?'default_ping_status'?option.
  15. ?*???????????????????????Whether?the?attachment?can?accept?pings.
  16. ?*?????'post_parent'???–?Default?is?0.?Set?this?for?the?post?it?belongs?to,?if?any.
  17. ?*?????'menu_order'????–?Default?is?0.?The?order?it?is?displayed.
  18. ?*?????'to_ping'???????–?Whether?to?ping.
  19. ?*?????'pinged'????????–?Default?is?empty?string.
  20. ?*?????'post_password'?–?Default?is?empty?string.?The?password?to?access?the?attachment.
  21. ?*?????'guid'??????????–?Global?Unique?ID?for?referencing?the?attachment.
  22. ?*?????'post_content_filtered'?–?Post?content?filtered.
  23. ?*?????'post_excerpt'??–?Post?excerpt.
  24. ?*
  25. ?*?@since?1.0.0
  26. ?*?@uses?$wpdb
  27. ?*?@uses?$wp_rewrite
  28. ?*?@uses?$user_ID
  29. ?*?@uses?do_action()?Calls?'pre_post_update'?on?post?ID?if?this?is?an?update.
  30. ?*?@uses?do_action()?Calls?'edit_post'?action?on?post?ID?and?post?data?if?this?is?an?update.
  31. ?*?@uses?do_action()?Calls?'save_post'?and?'wp_insert_post'?on?post?id?and?post?data?just?before?returning.
  32. ?*?@uses?apply_filters()?Calls?'wp_insert_post_data'?passing?$data,?$postarr?prior?to?database?update?or?insert.
  33. ?*?@uses?wp_transition_post_status()
  34. ?*
  35. ?*?@param?array?$postarr?Elements?that?make?up?post?to?insert.
  36. ?*?@param?bool?$wp_error?Optional.?Allow?return?of?WP_Error?on?failure.
  37. ?*?@return?int|WP_Error?The?value?0?or?WP_Error?on?failure.?The?post?ID?on?success.
  38. ?*/
  39. function?wp_insert_post($postarr,?$wp_error?=?false)?{
  40. ?global?$wpdb,?$wp_rewrite,?$user_ID;
  41. ?$defaults?=?array('post_status'?=>?'draft',?'post_type'?=>?'post',?'post_author'?=>?$user_ID,
  42. ??'ping_status'?=>?get_option('default_ping_status'),?'post_parent'?=>?0,
  43. ??'menu_order'?=>?0,?'to_ping'?=>??'',?'pinged'?=>?'',?'post_password'?=>?'',
  44. ??'guid'?=>?'',?'post_content_filtered'?=>?'',?'post_excerpt'?=>?'',?'import_id'?=>?0,
  45. ??'post_content'?=>?'',?'post_title'?=>?'');
  46. ?$postarr?=?wp_parse_args($postarr,?$defaults);
  47. ?unset(?$postarr[?'filter'?]?);
  48. ?$postarr?=?sanitize_post($postarr,?'db');
  49. ?//?export?array?as?variables
  50. ?extract($postarr,?EXTR_SKIP);
  51. ?//?Are?we?updating?or?creating?
  52. ?$update?=?false;
  53. ?if?(?!emptyempty($ID)?)?{
  54. ??$update?=?true;
  55. ??$previous_status?=?get_post_field('post_status',?$ID);
  56. ?}?else?{
  57. ??$previous_status?=?'new';
  58. ?}
  59. ?if?(?(''?==?$post_content)?&&?(''?==?$post_title)?&&?(''?==?$post_excerpt)?&&?('attachment'?!=?$post_type)?)?{
  60. ??if?(?$wp_error?)
  61. ???return?new?WP_Error('empty_content',?__('Content,?title,?and?excerpt?are?emptyempty.'));
  62. ??else
  63. ???return?0;
  64. ?}
  65. ?if?(?emptyempty($post_type)?)
  66. ??$post_type?=?'post';
  67. ?if?(?emptyempty($post_status)?)
  68. ??$post_status?=?'draft';
  69. ?if?(?!emptyempty($post_category)?)
  70. ??$post_category?=?array_filter($post_category);?//?Filter?out?empty?terms
  71. ?//?Make?sure?we?set?a?valid?category.
  72. ?if?(?emptyempty($post_category)?||?0?==?count($post_category)?||?!is_array($post_category)?)?{
  73. ??//?'post'?requires?at?least?one?category.
  74. ??if?(?'post'?==?$post_type?&&?'auto-draft'?!=?$post_status?)
  75. ???$post_category?=?array(?get_option('default_category')?);
  76. ??else
  77. ???$post_category?=?array();
  78. ?}
  79. ?if?(?emptyempty($post_author)?)
  80. ??$post_author?=?$user_ID;
  81. ?$post_ID?=?0;
  82. ?//?Get?the?post?ID?and?GUID
  83. ?if?(?$update?)?{
  84. ??$post_ID?=?(int)?$ID;
  85. ??$guid?=?get_post_field(?'guid',?$post_ID?);
  86. ??$post_before?=?get_post($post_ID);
  87. ?}
  88. ?//?Don't?allow?contributors?to?set?the?post?slug?for?pending?review?posts
  89. ?if?(?'pending'?==?$post_status?&&?!current_user_can(?'publish_posts'?)?)
  90. ??$post_name?=?'';
  91. ?//?Create?a?valid?post?name.??Drafts?and?pending?posts?are?allowed?to?have?an?empty
  92. ?//?post?name.
  93. ?if?(?emptyempty($post_name)?)?{
  94. ??if?(?!in_array(?$post_status,?array(?'draft',?'pending',?'auto-draft'?)?)?)
  95. ???$post_name?=?sanitize_title($post_title);
  96. ??else
  97. ???$post_name?=?'';
  98. ?}?else?{
  99. ??$post_name?=?sanitize_title($post_name);
  100. ?}
  101. ?//?If?the?post?date?is?empty?(due?to?having?been?new?or?a?draft)?and?status?is?not?'draft'?or?'pending',?set?date?to?now
  102. ?if?(?emptyempty($post_date)?||?'0000-00-00?00:00:00'?==?$post_date?)
  103. ??$post_date?=?current_time('mysql');
  104. ?if?(?emptyempty($post_date_gmt)?||?'0000-00-00?00:00:00'?==?$post_date_gmt?)?{
  105. ??if?(?!in_array(?$post_status,?array(?'draft',?'pending',?'auto-draft'?)?)?)
  106. ???$post_date_gmt?=?get_gmt_from_date($post_date);
  107. ??else
  108. ???$post_date_gmt?=?'0000-00-00?00:00:00';
  109. ?}
  110. ?if?(?$update?||?'0000-00-00?00:00:00'?==?$post_date?)?{
  111. ??$post_modified?????=?current_time(?'mysql'?);
  112. ??$post_modified_gmt?=?current_time(?'mysql',?1?);
  113. ?}?else?{
  114. ??$post_modified?????=?$post_date;
  115. ??$post_modified_gmt?=?$post_date_gmt;
  116. ?}
  117. ?if?(?'publish'?==?$post_status?)?{
  118. ??$now?=?gmdate('Y-m-d?H:i:59');
  119. ??if?(?mysql2date('U',?$post_date_gmt,?false)?>?mysql2date('U',?$now,?false)?)
  120. ???$post_status?=?'future';
  121. ?}?elseif(?'future'?==?$post_status?)?{
  122. ??$now?=?gmdate('Y-m-d?H:i:59');
  123. ??if?(?mysql2date('U',?$post_date_gmt,?false)?<=?mysql2date('U',?$now,?false)?)
  124. ???$post_status?=?'publish';
  125. ?}
  126. ?if?(?emptyempty($comment_status)?)?{
  127. ??if?(?$update?)
  128. ???$comment_status?=?'closed';
  129. ??else
  130. ???$comment_status?=?get_option('default_comment_status');
  131. ?}
  132. ?if?(?emptyempty($ping_status)?)
  133. ??$ping_status?=?get_option('default_ping_status');
  134. ?if?(?isset($to_ping)?)
  135. ??$to_ping?=?preg_replace('|\s+|',?"\n",?$to_ping);
  136. ?else
  137. ??$to_ping?=?'';
  138. ?if?(?!?isset($pinged)?)
  139. ??$pinged?=?'';
  140. ?if?(?isset($post_parent)?)
  141. ??$post_parent?=?(int)?$post_parent;
  142. ?else
  143. ??$post_parent?=?0;
  144. ?//?Check?the?post_parent?to?see?if?it?will?cause?a?hierarchy?loop
  145. ?$post_parent?=?apply_filters(?'wp_insert_post_parent',?$post_parent,?$post_ID,?compact(?array_keys(?$postarr?)?),?$postarr?);
  146. ?if?(?isset($menu_order)?)
  147. ??$menu_order?=?(int)?$menu_order;
  148. ?else
  149. ??$menu_order?=?0;
  150. ?if?(?!isset($post_password)?||?'private'?==?$post_status?)
  151. ??$post_password?=?'';
  152. ?$post_name?=?wp_unique_post_slug($post_name,?$post_ID,?$post_status,?$post_type,?$post_parent);
  153. ?//?expected_slashed?(everything!)
  154. ?$data?=?compact(?array(?'post_author',?'post_date',?'post_date_gmt',?'post_content',?'post_content_filtered',?'post_title',?'post_excerpt',?'post_status',?'post_type',?'comment_status',?'ping_status',?'post_password',?'post_name',?'to_ping',?'pinged',?'post_modified',?'post_modified_gmt',?'post_parent',?'menu_order',?'guid'?)?);
  155. ?$data?=?apply_filters('wp_insert_post_data',?$data,?$postarr);
  156. ?$data?=?stripslashes_deep(?$data?);
  157. ?$where?=?array(?'ID'?=>?$post_ID?);
  158. ?if?(?$update?)?{
  159. ??do_action(?'pre_post_update',?$post_ID?);
  160. ??if?(?false?===?$wpdb->update(?$wpdb->posts,?$data,?$where?)?)?{
  161. ???if?(?$wp_error?)
  162. ????return?new?WP_Error('db_update_error',?__('Could?not?update?post?in?the?database'),?$wpdb->last_error);
  163. ???else
  164. ????return?0;
  165. ??}
  166. ?}?else?{
  167. ??if?(?isset($post_mime_type)?)
  168. ???$data['post_mime_type']?=?stripslashes(?$post_mime_type?);?//?This?isn't?in?the?update
  169. ??//?If?there?is?a?suggested?ID,?use?it?if?not?already?present
  170. ??if?(?!emptyempty($import_id)?)?{
  171. ???$import_id?=?(int)?$import_id;
  172. ???if?(?!?$wpdb->get_var(?$wpdb->prepare("SELECT?ID?FROM?$wpdb->posts?WHERE?ID?=?%d",?$import_id)?)?)?{
  173. ????$data['ID']?=?$import_id;
  174. ???}
  175. ??}
  176. ??if?(?false?===?$wpdb->insert(?$wpdb->posts,?$data?)?)?{
  177. ???if?(?$wp_error?)
  178. ????return?new?WP_Error('db_insert_error',?__('Could?not?insert?post?into?the?database'),?$wpdb->last_error);
  179. ???else
  180. ????return?0;
  181. ??}
  182. ??$post_ID?=?(int)?$wpdb->insert_id;
  183. ??//?use?the?newly?generated?$post_ID
  184. ??$where?=?array(?'ID'?=>?$post_ID?);
  185. ?}
  186. ?if?(?emptyempty($data['post_name'])?&&?!in_array(?$data['post_status'],?array(?'draft',?'pending',?'auto-draft'?)?)?)?{
  187. ??$data['post_name']?=?sanitize_title($data['post_title'],?$post_ID);
  188. ??$wpdb->update(?$wpdb->posts,?array(?'post_name'?=>?$data['post_name']?),?$where?);
  189. ?}
  190. ?if?(?is_object_in_taxonomy($post_type,?'category')?)
  191. ??wp_set_post_categories(?$post_ID,?$post_category?);
  192. ?if?(?isset(?$tags_input?)?&&?is_object_in_taxonomy($post_type,?'post_tag')?)
  193. ??wp_set_post_tags(?$post_ID,?$tags_input?);
  194. ?//?new-style?support?for?all?custom?taxonomies
  195. ?if?(?!emptyempty($tax_input)?)?{
  196. ??foreach?(?$tax_input?as?$taxonomy?=>?$tags?)?{
  197. ???$taxonomy_obj?=?get_taxonomy($taxonomy);
  198. ???if?(?is_array($tags)?)?//?array?=?hierarchical,?string?=?non-hierarchical.
  199. ????$tags?=?array_filter($tags);
  200. ???if?(?current_user_can($taxonomy_obj->cap->assign_terms)?)
  201. ????wp_set_post_terms(?$post_ID,?$tags,?$taxonomy?);
  202. ??}
  203. ?}
  204. ?$current_guid?=?get_post_field(?'guid',?$post_ID?);
  205. ?if?(?'page'?==?$data['post_type']?)
  206. ??clean_page_cache($post_ID);
  207. ?else
  208. ??clean_post_cache($post_ID);
  209. ?//?Set?GUID
  210. ?if?(?!$update?&&?''?==?$current_guid?)
  211. ??$wpdb->update(?$wpdb->posts,?array(?'guid'?=>?get_permalink(?$post_ID?)?),?$where?);
  212. ?$post?=?get_post($post_ID);
  213. ?if?(?!emptyempty($page_template)?&&?'page'?==?$data['post_type']?)?{
  214. ??$post->page_template?=?$page_template;
  215. ??$page_templates?=?get_page_templates();
  216. ??if?(?'default'?!=?$page_template?&&?!in_array($page_template,?$page_templates)?)?{
  217. ???if?(?$wp_error?)
  218. ????return?new?WP_Error('invalid_page_template',?__('The?page?template?is?invalid.'));
  219. ???else
  220. ????return?0;
  221. ??}
  222. ??update_post_meta($post_ID,?'_wp_page_template',??$page_template);
  223. ?}
  224. ?wp_transition_post_status($data['post_status'],?$previous_status,?$post);
  225. ?if?(?$update?)?{
  226. ??do_action('edit_post',?$post_ID,?$post);
  227. ??$post_after?=?get_post($post_ID);
  228. ??do_action(?'post_updated',?$post_ID,?$post_after,?$post_before);
  229. ?}
  230. ?do_action('save_post',?$post_ID,?$post);
  231. ?do_action('wp_insert_post',?$post_ID,?$post);
  232. ?return?$post_ID;
  233. }
4 0

企業(yè)建站推薦正版商業(yè)主題,國(guó)內(nèi)專業(yè)團(tuán)隊(duì)開(kāi)發(fā),完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計(jì)幫助1300+客戶成功建站,為站長(zhǎng)提供支持!

立刻開(kāi)啟你的建站之旅
QQ在線客服

服務(wù)熱線

wordpress建站咨詢