From 4a67a9774a3c1bfb8e9ce30ba05705c324d6fc97 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 4 Nov 2021 23:55:21 +0000 Subject: [PATCH] Comments: Avoid reparenting during post deletion. Delete comments in a descending order by comment ID when deleting a post. This avoids the expense of additional database queries required to re-parent threaded comments that are themselves about to be deleted. Props Mte90, andraganescu, johnbillion, hellofromTonya, peterwilsoncc. Fixes #37703. git-svn-id: https://develop.svn.wordpress.org/trunk@52015 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 0f75bf537e..b6ca7c24d3 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3236,7 +3236,7 @@ function wp_delete_post( $postid = 0, $force_delete = false ) { wp_defer_comment_counting( true ); - $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ) ); + $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $postid ) ); foreach ( $comment_ids as $comment_id ) { wp_delete_comment( $comment_id, true ); } @@ -6194,7 +6194,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) { wp_defer_comment_counting( true ); - $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); + $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) ); foreach ( $comment_ids as $comment_id ) { wp_delete_comment( $comment_id, true ); }