Skip to content

Commit a451b7e

Browse files
committed
Optimize the total count queries in REST API
1 parent 8879667 commit a451b7e

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ public function get_items( $request ) {
295295
// Out-of-bounds, run the query again without LIMIT for total count.
296296
unset( $prepared_args['number'], $prepared_args['offset'] );
297297

298-
$query = new WP_Comment_Query();
299-
$prepared_args['count'] = true;
300-
$prepared_args['orderby'] = 'none';
298+
$query = new WP_Comment_Query();
299+
$prepared_args['count'] = true;
300+
$prepared_args['orderby'] = 'none';
301+
$prepared_args['update_comment_meta_cache'] = false;
301302

302303
$total_comments = $query->query( $prepared_args );
303304
$max_pages = (int) ceil( $total_comments / $request['per_page'] );

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,13 @@ public function get_items( $request ) {
195195
if ( $total_revisions < 1 ) {
196196
// Out-of-bounds, run the query again without LIMIT for total count.
197197
unset( $query_args['paged'], $query_args['offset'] );
198-
$count_query = new WP_Query();
198+
199+
$count_query = new WP_Query();
200+
$query_args['fields'] = 'ids';
201+
$query_args['posts_per_page'] = 1;
202+
$query_args['update_post_meta_cache'] = false;
203+
$query_args['update_post_term_cache'] = false;
204+
199205
$count_query->query( $query_args );
200206

201207
$total_revisions = $count_query->found_posts;

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ static function ( $format ) {
464464
// Out-of-bounds, run the query again without LIMIT for total count.
465465
unset( $query_args['paged'] );
466466

467-
$count_query = new WP_Query();
467+
$count_query = new WP_Query();
468+
$query_args['fields'] = 'ids';
469+
$query_args['posts_per_page'] = 1;
470+
$query_args['update_post_meta_cache'] = false;
471+
$query_args['update_post_term_cache'] = false;
472+
468473
$count_query->query( $query_args );
469474
$total_posts = $count_query->found_posts;
470475
}

src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,10 @@ public function get_items( $request ) {
367367
if ( $total_users < 1 ) {
368368
// Out-of-bounds, run the query again without LIMIT for total count.
369369
unset( $prepared_args['number'], $prepared_args['offset'] );
370-
$count_query = new WP_User_Query( $prepared_args );
371-
$total_users = $count_query->get_total();
370+
371+
$prepared_args['number'] = 1;
372+
$count_query = new WP_User_Query( $prepared_args );
373+
$total_users = $count_query->get_total();
372374
}
373375

374376
$response->header( 'X-WP-Total', (int) $total_users );

0 commit comments

Comments
 (0)