From cf7279320b14b8a370d2f20eaac77645172dcf80 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 4 Nov 2009 17:39:53 +0000 Subject: [PATCH] Fix comment and postmeta delete queries when deleting a post. see #11073 git-svn-id: http://svn.automattic.com/wordpress/trunk@12141 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post.php | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index f1fc878e52..f63f364b08 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -1202,15 +1202,21 @@ function wp_delete_post($postid = 0) { // Point all attachments to this post up one level $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); - $commentids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); - do_action( 'delete_comment', $commentids ); - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_ID IN(%s)", implode( ',', $commentids ) )); - do_action( 'deleted_comment', $commentids ); + $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); + if ( ! empty($comment_ids) ) { + do_action( 'delete_comment', $comment_ids ); + $in_comment_ids = "'" . implode("', '", $comment_ids) . "'"; + $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_ID IN($in_comment_ids)" ); + do_action( 'deleted_comment', $comment_ids ); + } - $postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); - do_action( 'delete_postmeta', $postmetaids ); - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $postmetaids ) )); - do_action( 'deleted_postmeta', $postmetaids ); + $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid )); + if ( !empty($post_meta_ids) ) { + do_action( 'delete_postmeta', $post_meta_ids ); + $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; + $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" ); + do_action( 'deleted_postmeta', $post_meta_ids ); + } do_action( 'delete_post', $post_id ); $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid )); @@ -2729,15 +2735,21 @@ function wp_delete_attachment($post_id) { $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id )); - $commentids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); - do_action( 'delete_comment', $commentids ); - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_ID IN(%s)", implode( ',', $commentids ) )); - do_action( 'deleted_comment', $commentids ); + $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); + if ( ! empty($comment_ids) ) { + do_action( 'delete_comment', $comment_ids ); + $in_comment_ids = "'" . implode("', '", $comment_ids) . "'"; + $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_ID IN($in_comment_ids)" ); + do_action( 'deleted_comment', $comment_ids ); + } - $postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); - do_action( 'delete_postmeta', $postmetaids ); - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $postmetaids ) )); - do_action( 'deleted_postmeta', $postmetaids ); + $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); + if ( !empty($post_meta_ids) ) { + do_action( 'delete_postmeta', $post_meta_ids ); + $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; + $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" ); + do_action( 'deleted_postmeta', $post_meta_ids ); + } do_action( 'delete_post', $post_id ); $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id ));