Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Optimize total count queries in controllers #8113

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,10 @@ public function get_items( $request ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $prepared_args['number'], $prepared_args['offset'] );

$query = new WP_Comment_Query();
$prepared_args['count'] = true;
$prepared_args['orderby'] = 'none';
$query = new WP_Comment_Query();
$prepared_args['count'] = true;
$prepared_args['orderby'] = 'none';
$prepared_args['update_comment_meta_cache'] = false;

$total_comments = $query->query( $prepared_args );
$max_pages = (int) ceil( $total_comments / $request['per_page'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ public function get_items( $request ) {
if ( $total_revisions < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'], $query_args['offset'] );
$count_query = new WP_Query();

$count_query = new WP_Query();
$query_args['fields'] = 'ids';
$query_args['posts_per_page'] = 1;
$query_args['update_post_meta_cache'] = false;
$query_args['update_post_term_cache'] = false;

$count_query->query( $query_args );

$total_revisions = $count_query->found_posts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ static function ( $format ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] );

$count_query = new WP_Query();
$count_query = new WP_Query();
$query_args['fields'] = 'ids';
$query_args['posts_per_page'] = 1;
$query_args['update_post_meta_cache'] = false;
$query_args['update_post_term_cache'] = false;

$count_query->query( $query_args );
$total_posts = $count_query->found_posts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ public function get_items( $request ) {
if ( $total_users < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $prepared_args['number'], $prepared_args['offset'] );
$count_query = new WP_User_Query( $prepared_args );
$total_users = $count_query->get_total();

$prepared_args['number'] = 1;
$count_query = new WP_User_Query( $prepared_args );
$total_users = $count_query->get_total();
}

$response->header( 'X-WP-Total', (int) $total_users );
Expand Down
Loading