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.


Built from https://develop.svn.wordpress.org/trunk@52015


git-svn-id: http://core.svn.wordpress.org/trunk@51606 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2021-11-04 23:56:58 +00:00
parent 9036739acc
commit cae71f232c
2 changed files with 3 additions and 3 deletions

View File

@ -3236,7 +3236,7 @@ function wp_delete_post( $postid = 0, $force_delete = false ) {
wp_defer_comment_counting( true ); 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 ) { foreach ( $comment_ids as $comment_id ) {
wp_delete_comment( $comment_id, true ); wp_delete_comment( $comment_id, true );
} }
@ -6194,7 +6194,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
wp_defer_comment_counting( true ); 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 ) { foreach ( $comment_ids as $comment_id ) {
wp_delete_comment( $comment_id, true ); wp_delete_comment( $comment_id, true );
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-52014'; $wp_version = '5.9-alpha-52015';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.