Fix 40075/avoid count query unless after first page (#40092)

* Avoid running a second query to account for incorrect totals if pagination is set too high if pagination isn't used.

Fixes #40075

* Adding changelog entry
This commit is contained in:
Michael Pretty 2023-12-01 06:44:56 -05:00 committed by GitHub
parent 34cfbd94b4
commit 874bffc0db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: performance
Prevent second query used to determine real post count from running if paging isn't being used.

View File

@ -354,7 +354,7 @@ abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller {
$result = $query->query( $query_args ); $result = $query->query( $query_args );
$total_posts = $query->found_posts; $total_posts = $query->found_posts;
if ( $total_posts < 1 ) { if ( $total_posts < 1 && isset( $query_args['paged'] ) && absint( $query_args['paged'] ) > 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count. // Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] ); unset( $query_args['paged'] );
$count_query = new WP_Query(); $count_query = new WP_Query();

View File

@ -367,7 +367,7 @@ abstract class WC_REST_Posts_Controller extends WC_REST_Controller {
$page = (int) $query_args['paged']; $page = (int) $query_args['paged'];
$total_posts = $posts_query->found_posts; $total_posts = $posts_query->found_posts;
if ( $total_posts < 1 ) { if ( $total_posts < 1 && $page > 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count. // Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] ); unset( $query_args['paged'] );
$count_query = new WP_Query(); $count_query = new WP_Query();

View File

@ -93,7 +93,7 @@ class Controller extends GenericController implements ExportableInterface {
$result = $query->query( $query_args ); $result = $query->query( $query_args );
$total_posts = $query->found_posts; $total_posts = $query->found_posts;
if ( $total_posts < 1 ) { if ( $total_posts < 1 && isset( $query_args['paged'] ) && absint( $query_args['paged'] ) > 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count. // Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] ); unset( $query_args['paged'] );
$count_query = new \WP_Query(); $count_query = new \WP_Query();