Opened 6 years ago
Last modified 6 years ago
#44694 new enhancement
Extra and un-neccessary query when no posts found in Posts Rest Controller
Reported by: | Owned by: | ||
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 4.9.7 |
Component: | REST API | Keywords: | needs-patch |
Focuses: | rest-api, performance | Cc: |
Description
In the 'get_items' method in class-wp-rest-posts-controller.php there is some conditional logic that re-executes the query without the 'paged' param if no results are found ( line 317 ).
This seems like poor logic here. The query should only be executed again IF a paged parameter has been specified in the params.
I have a REST request that returns an empty array, but the query is run twice even though the results are the same.
This leads to some errors for me because I'm supplying non-standard parameters and using one time filters to handle them when building the request. After the initial query my filters have been removed so when the query executes again ( un-neccessarily ) my objects in the params end up triggering notices / warnings in the WP_Query class.
Long story short, this extra query should have better logic protecting it.
Something as simple as:
<?php if ( $total_posts < 1 && $paged > 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; }
Out of curiosity:
What if
$paged
parameter has been specified$page = 1
and
$total_posts = 0