WordPress删除文章函数:wp_delete_post


【描述】
删除一个文章,附件,或者页面.
当在文章或页面使用这个函数的时候,所有依赖这个文章或页面的内容也将被删除。包括评论,自定义字段,和文章与类别的关系

【使用方法】

  1. <?php wp_delete_post( $postid, $force_delete ); ?>

【参数】

$postid
(整形)(可选) 文章 ID.
默认: 0

$force_delete
(布尔型) (可选) 是否绕过回收站箱直接删除 (在 WordPress 2.9 添加).
默认: false
返回值
混合型(mixed)
False on failure and a random wpdb object on success.

【例子】
删除文章
删除默认文章 “Hello World” 文章 ID 是 ‘1’.

  1. <?php wp_delete_post(1); ?>

【源文件】
wp_delete_post() 位于 wp-includes/post.php.

  1. /**
  2. * Trashes or deletes a post or page.
  3. *
  4. * When the post and page is permanently deleted, everything that is tied to it is deleted also.
  5. * This includes comments, post meta fields, and terms associated with the post.
  6. *
  7. * The post or page is moved to trash instead of permanently deleted unless trash is
  8. * disabled, item is already in the trash, or $force_delete is true.
  9. *
  10. * @since 1.0.0
  11. * @uses do_action() on 'delete_post' before deletion unless post type is 'attachment'.
  12. * @uses do_action() on 'deleted_post' after deletion unless post type is 'attachment'.
  13. * @uses wp_delete_attachment() if post type is 'attachment'.
  14. * @uses wp_trash_post() if item should be trashed.
  15. *
  16. * @param int $postid Post ID.
  17. * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
  18. * @return mixed False on failure
  19. */
  20. function wp_delete_post( $postid = 0, $force_delete = false ) {
  21. global $wpdb, $wp_rewrite;
  22.  
  23. if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
  24. return $post;
  25.  
  26. if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS )
  27. return wp_trash_post($postid);
  28.  
  29. if ( $post->post_type == 'attachment' )
  30. return wp_delete_attachment( $postid, $force_delete );
  31.  
  32. do_action('before_delete_post', $postid);
  33.  
  34. delete_post_meta($postid,'_wp_trash_meta_status');
  35. delete_post_meta($postid,'_wp_trash_meta_time');
  36.  
  37. wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
  38.  
  39. $parent_data = array( 'post_parent' => $post->post_parent );
  40. $parent_where = array( 'post_parent' => $postid );
  41.  
  42. if ( 'page' == $post->post_type) {
  43. // if the page is defined in option page_on_front or post_for_posts,
  44. // adjust the corresponding options
  45. if ( get_option('page_on_front') == $postid ) {
  46. update_option('show_on_front', 'posts');
  47. delete_option('page_on_front');
  48. }
  49. if ( get_option('page_for_posts') == $postid ) {
  50. delete_option('page_for_posts');
  51. }
  52.  
  53. // Point children of this page to its parent, also clean the cache of affected children
  54. $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid);
  55. $children = $wpdb->get_results($children_query);
  56.  
  57. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) );
  58. } else {
  59. unstick_post($postid);
  60. }
  61.  
  62. // Do raw query. wp_get_post_revisions() is filtered
  63. $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
  64. // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
  65. foreach ( $revision_ids as $revision_id )
  66. wp_delete_post_revision( $revision_id );
  67.  
  68. // Point all attachments to this post up one level
  69. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
  70.  
  71. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
  72. if ( ! empty($comment_ids) ) {
  73. do_action( 'delete_comment', $comment_ids );
  74. foreach ( $comment_ids as $comment_id )
  75. wp_delete_comment( $comment_id, true );
  76. do_action( 'deleted_comment', $comment_ids );
  77. }
  78.  
  79. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
  80. if ( !empty($post_meta_ids) ) {
  81. do_action( 'delete_postmeta', $post_meta_ids );
  82. $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'";
  83. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" );
  84. do_action( 'deleted_postmeta', $post_meta_ids );
  85. }
  86.  
  87. do_action( 'delete_post', $postid );
  88. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
  89. do_action( 'deleted_post', $postid );
  90.  
  91. if ( 'page' == $post->post_type ) {
  92. clean_page_cache($postid);
  93.  
  94. foreach ( (array) $children as $child )
  95. clean_page_cache($child->ID);
  96.  
  97. $wp_rewrite->flush_rules(false);
  98. } else {
  99. clean_post_cache($postid);
  100. }
  101.  
  102. wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
  103.  
  104. do_action('after_delete_post', $postid);
  105.  
  106. return $post;
  107. }