[wp-trac] [WordPress Trac] #50213: REST API: Allow queries other than the main query to be `is_home`
WordPress Trac
noreply at wordpress.org
Wed May 20 14:42:07 UTC 2020
#50213: REST API: Allow queries other than the main query to be `is_home`
--------------------------+-----------------------------
Reporter: dlh | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version: 4.4
Severity: normal | Keywords: has-patch
Focuses: |
--------------------------+-----------------------------
[35690] prevented `WP_Query` from falling back to `is_home` during REST
requests.
ticket:34373#comment:2 noted:
> If WP_Query hardcodes an is_rest, it's going to mean some awkward
workarounds in the case where one actually wants to make a 'home' query in
a REST request.
And that is the situation I present now :)
My use case is a REST API endpoint that provides data to a mobile
application in which some screens mirror the website's frontend.
Within the route callback, it's possible to create a category or singular
query using `new WP_Query( $args )` and to pass those queries to existing
functions that return data based on `$query->is_category()`,
`$query->is_singular()`, etc. But it's currently not possible to reuse
existing functions that check `$query->is_home()` unless the `is_home`
property is set by hand. For example:
{{{
$query = new \WP_Query();
add_action(
'parse_query',
function ( $q ) use ( $query ) {
if ( $q === $query ) {
$q->is_home = true;
}
}
);
}}}
The attached patch proposes to limit the `is_home` restriction to the main
query, which I think would allow for secondary queries to behave normally
while respecting the spirit of [35690].
--
Ticket URL: <https://core.trac.wordpress.org/ticket/50213>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list