Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class WP_Query {
*/
public $request;

/**
* Get post database count query
*
* @since xxx
* @var string
*/
public $request_count;

/**
* List of posts.
*
Expand Down Expand Up @@ -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'] ) {
/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down
13 changes: 6 additions & 7 deletions src/wp-includes/class-wp-user-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand All @@ -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 );
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/query/noFoundRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down