From dfa778796072b98453040644e5f4de2c997da541 Mon Sep 17 00:00:00 2001 From: Vladimir Reznichenko Date: Thu, 15 Jan 2026 09:43:02 +0100 Subject: [PATCH 1/2] wp_dashboard_recent_comments function: avoid unnecessary SQL query --- src/wp-admin/includes/dashboard.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index cac03b1fe3630..d6f52b13c0927 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -1076,7 +1076,8 @@ function wp_dashboard_recent_comments( $total_items = 5 ) { $comments_count = 0; do { - $possible = get_comments( $comments_query ); + $fetch_count = $comments_query['number'] - $comments_query['offset']; + $possible = get_comments( $comments_query ); if ( empty( $possible ) || ! is_array( $possible ) ) { break; @@ -1091,8 +1092,8 @@ function wp_dashboard_recent_comments( $total_items = 5 ) { continue; } - $comments[] = $comment; - $comments_count = count( $comments ); + $comments[] = $comment; + ++$comments_count; if ( $comments_count === $total_items ) { break 2; @@ -1101,7 +1102,7 @@ function wp_dashboard_recent_comments( $total_items = 5 ) { $comments_query['offset'] += $comments_query['number']; $comments_query['number'] = $total_items * 10; - } while ( $comments_count < $total_items ); + } while ( $comments_count < $total_items && $fetch_count === count( $possible ) ); if ( $comments ) { echo '
'; From b9f4705ff74a954f78bb98361738d3efe92f2c24 Mon Sep 17 00:00:00 2001 From: Vladimir Reznichenko Date: Thu, 15 Jan 2026 09:48:15 +0100 Subject: [PATCH 2/2] wp_dashboard_recent_comments function: code style tweaks --- src/wp-admin/includes/dashboard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index d6f52b13c0927..35402023e8f13 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -1102,7 +1102,7 @@ function wp_dashboard_recent_comments( $total_items = 5 ) { $comments_query['offset'] += $comments_query['number']; $comments_query['number'] = $total_items * 10; - } while ( $comments_count < $total_items && $fetch_count === count( $possible ) ); + } while ( $comments_count < $total_items && count( $possible ) === $fetch_count ); if ( $comments ) { echo '
';