diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 5027f21cf0ade..81ca01c7f38c7 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -83,6 +83,14 @@ class WP_Query { */ public $request; + /** + * Get post database count query + * + * @since xxx + * @var string + */ + public $request_count; + /** * List of posts. * @@ -2910,13 +2918,9 @@ public function get_posts() { $orderby = 'ORDER BY ' . $orderby; } - $found_rows = ''; - if ( ! $q['no_found_rows'] && ! empty( $limits ) ) { - $found_rows = 'SQL_CALC_FOUND_ROWS'; - } - - $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + $old_request = "SELECT $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; $this->request = $old_request; + $this->request_count = "SELECT COUNT($distinct {$wpdb->posts}.ID) FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby"; if ( ! $q['suppress_filters'] ) { /** @@ -2999,7 +3003,7 @@ public function get_posts() { if ( $split_the_query ) { // First get the IDs and then fill in the objects. - $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; + $this->request = "SELECT $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; /** * Filters the Post IDs SQL request before sending. @@ -3238,7 +3242,7 @@ private function set_found_posts( $q, $limits ) { * @param string $found_posts The query to run to find the found posts. * @param WP_Query $this The WP_Query instance (passed by reference). */ - $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) ); + $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( $this->request_count, &$this ) ) ); } else { if ( is_array( $this->posts ) ) { $this->found_posts = count( $this->posts ); diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index 02b0ff3814498..ce2eac02e271d 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -248,10 +248,6 @@ public function prepare_query( $query = array() ) { $this->query_fields = "$wpdb->users.ID"; } - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; - } - $this->query_from = "FROM $wpdb->users"; $this->query_where = 'WHERE 1=1'; @@ -621,8 +617,12 @@ public function query() { } if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { + /** - * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance. + * Return a total count of users. + * Historically this ran SELECT FOUND_ROWS() after issuing the main query, + * but since SQL_CALC_FOUND_ROWS is now deprecated from MySQL, it instead repeats + * the same query with COUNT(*) instead of $this->query_fields. * * @since 3.2.0 * @since 5.1.0 Added the `$this` parameter. @@ -632,8 +632,7 @@ public function query() { * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query. * @param WP_User_Query $this The current WP_User_Query instance. */ - $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this ); - + $found_users_query = apply_filters( 'found_users_query', "SELECT COUNT(*) $this->query_from $this->query_where", $this ); $this->total_users = (int) $wpdb->get_var( $found_users_query ); } } diff --git a/tests/phpunit/tests/query/noFoundRows.php b/tests/phpunit/tests/query/noFoundRows.php index 7731194d49752..e39f767da743f 100644 --- a/tests/phpunit/tests/query/noFoundRows.php +++ b/tests/phpunit/tests/query/noFoundRows.php @@ -11,7 +11,7 @@ public function test_no_found_rows_default() { ) ); - $this->assertContains( 'SQL_CALC_FOUND_ROWS', $q->request ); + $this->assertNotContains( 'SQL_CALC_FOUND_ROWS', $q->request ); } public function test_no_found_rows_false() { @@ -22,7 +22,7 @@ public function test_no_found_rows_false() { ) ); - $this->assertContains( 'SQL_CALC_FOUND_ROWS', $q->request ); + $this->assertNotContains( 'SQL_CALC_FOUND_ROWS', $q->request ); } public function test_no_found_rows_0() { @@ -33,7 +33,7 @@ public function test_no_found_rows_0() { ) ); - $this->assertContains( 'SQL_CALC_FOUND_ROWS', $q->request ); + $this->assertNotContains( 'SQL_CALC_FOUND_ROWS', $q->request ); } public function test_no_found_rows_empty_string() { @@ -44,7 +44,7 @@ public function test_no_found_rows_empty_string() { ) ); - $this->assertContains( 'SQL_CALC_FOUND_ROWS', $q->request ); + $this->assertNotContains( 'SQL_CALC_FOUND_ROWS', $q->request ); } public function test_no_found_rows_true() {