[wp-trac] [WordPress Trac] #44694: Extra and un-neccessary query when no posts found in Posts Rest Controller
WordPress Trac
noreply at wordpress.org
Wed Aug 1 16:46:27 UTC 2018
#44694: Extra and un-neccessary query when no posts found in Posts Rest Controller
-------------------------------------------------+-------------------------
Reporter: pat@… | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting
| Review
Component: REST API | Version: 4.9.7
Severity: normal | Keywords: needs-patch
Focuses: rest-api, performance, coding- |
standards |
-------------------------------------------------+-------------------------
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
<?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;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/44694>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list