Skip to content

Commit af59be8

Browse files
committed
Refactor WP_Query to remove SQL_CALC_FOUND_ROWS and implement COUNT query logic.
1 parent 8508427 commit af59be8

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/wp-includes/class-wp-query.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ class WP_Query {
485485
*/
486486
private $query_cache_key = '';
487487

488+
/**
489+
* @var string
490+
*/
491+
private $count_request;
492+
488493
/**
489494
* Resets query flags to false.
490495
*
@@ -3162,18 +3167,15 @@ public function get_posts() {
31623167
$limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
31633168
}
31643169

3170+
$count_field = "{$wpdb->posts}.ID";
31653171
if ( ! empty( $groupby ) ) {
3166-
$groupby = 'GROUP BY ' . $groupby;
3172+
$count_field = $groupby;
3173+
$groupby = 'GROUP BY ' . $groupby;
31673174
}
31683175
if ( ! empty( $orderby ) ) {
31693176
$orderby = 'ORDER BY ' . $orderby;
31703177
}
31713178

3172-
$found_rows = '';
3173-
if ( ! $query_vars['no_found_rows'] && ! empty( $limits ) ) {
3174-
$found_rows = 'SQL_CALC_FOUND_ROWS';
3175-
}
3176-
31773179
/*
31783180
* Beginning of the string is on a new line to prevent leading whitespace.
31793181
*
@@ -3186,7 +3188,7 @@ public function get_posts() {
31863188
* See https://github.com/WordPress/wordpress-develop/pull/6393#issuecomment-2088217429
31873189
*/
31883190
$old_request =
3189-
"SELECT $found_rows $distinct $fields
3191+
"SELECT $distinct $fields
31903192
FROM {$wpdb->posts} $join
31913193
WHERE 1=1 $where
31923194
$groupby
@@ -3195,6 +3197,11 @@ public function get_posts() {
31953197

31963198
$this->request = $old_request;
31973199

3200+
$this->count_request =
3201+
"SELECT COUNT(DISTINCT {$count_field})
3202+
FROM {$wpdb->posts} $join
3203+
WHERE 1=1 $where";
3204+
31983205
if ( ! $query_vars['suppress_filters'] ) {
31993206
/**
32003207
* Filters the completed SQL query before sending.
@@ -3408,7 +3415,7 @@ public function get_posts() {
34083415

34093416
// Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
34103417
$this->request =
3411-
"SELECT $found_rows $distinct {$wpdb->posts}.ID
3418+
"SELECT $distinct {$wpdb->posts}.ID
34123419
FROM {$wpdb->posts} $join
34133420
WHERE 1=1 $where
34143421
$groupby
@@ -3689,6 +3696,12 @@ private function set_found_posts( $query_vars, $limits ) {
36893696
}
36903697

36913698
if ( ! empty( $limits ) ) {
3699+
$old_count_request = $this->count_request;
3700+
// If we used SQL_CALC_FOUND_ROWS, we need to make sure to use FOUND_ROWS() to get the correct count.
3701+
if ( str_contains( $this->request, 'SQL_CALC_FOUND_ROWS' ) ) {
3702+
$this->count_request = 'SELECT FOUND_ROWS()';
3703+
}
3704+
36923705
/**
36933706
* Filters the query to run for retrieving the found posts.
36943707
*
@@ -3697,7 +3710,12 @@ private function set_found_posts( $query_vars, $limits ) {
36973710
* @param string $found_posts_query The query to run to find the found posts.
36983711
* @param WP_Query $query The WP_Query instance (passed by reference).
36993712
*/
3700-
$found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
3713+
$found_posts_query = apply_filters_ref_array( 'found_posts_query', array( $this->count_request, &$this ) );
3714+
3715+
// If we changed the count_request to use FOUND_ROWS() but the found_posts_query filter reverted it back, restore the original count_request.
3716+
if ( ! str_contains( $this->request, 'SQL_CALC_FOUND_ROWS' ) && $found_posts_query === 'SELECT FOUND_ROWS()' ) {
3717+
$this->count_request = $old_count_request;
3718+
}
37013719

37023720
$this->found_posts = (int) $wpdb->get_var( $found_posts_query );
37033721
} else {

0 commit comments

Comments
 (0)