Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 11 days ago

#50213 closed defect (bug) (fixed)

REST API: Allow queries other than the main query to be `is_home`

Reported by: dlh Owned by: whyisjake
Priority: normal Milestone: 5.5
Component: REST API Version: 4.4
Severity: normal Keywords: commit has-patch has-unit-tests
Cc: Focuses:

Description

[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].

Attachments (1)

50213.diff​ (606 bytes ) - added by dlh 6 years ago.

Download all attachments as: .zip

Change History (11)

#1 @TimothyBlynJacobs
6 years ago

This makes sense to me from a REST API perspective. Maybe @boonebgorges has some thoughts from a query perspective?

#2 @boonebgorges
6 years ago

  • Keywords commit added
  • Milestone Awaiting Review → 5.5

@dlh Thanks as always for the very clear restatement of the problem. I agree with your assessment that 50213.diff​ respects the spirit of [35690].

#3 @joemcgill
6 years ago

I have a totally different use case from the one @dlh described above, but which has run into a similar problem. The solution in 50213.diff​ would work for me too. +1

#4 @whyisjake
6 years ago

  • Keywords needs-unit-tests added

@dlh, this changeset looks great. Can we add a test here to test this conditional?

#5 @whyisjake
6 years ago

  • Owner set to whyisjake
  • Status new → accepted

#6 @dlh
6 years ago

@whyisjake As this change depends on the value of a constant, I'm not sure how to reliably test it. Is there any prior art in the PHP unit tests that I'm overlooking?

#7 @whyisjake
6 years ago

  • Resolution → fixed
  • Status accepted → closed

In 48053:

REST API: Allow queries other than the main query to be is_home.

Ensure that REST API calls can match the main query of the page.

See #34373.

Fixes #50213.

Props: dlh, TimothyBlynJacobs, boonebgorges, joemcgill, whyisjake.

This ticket was mentioned in ​PR #14797 on ​WordPress/wordpress-develop by ​@irozum.


3 weeks ago
#8

  • Keywords has-unit-tests added; needs-unit-tests removed

Adds unit tests for the REST clause in WP_Query::parse_query(): ( wp_is_serving_rest_request() && $this->is_main_query() ). This is a test-only change and adds no production behaviour.

Follow-up to [48053], which restricted the REST exemption to the main query so that secondary queries can still be is_home. The ticket was closed as fixed with needs-unit-tests still set, and nothing in tests/phpunit covered it.

What is tested (Tests_Query_ParseQuery)

  • test_secondary_query_during_rest_request_can_be_home: with REST_REQUEST true, a query that is not $wp_the_query has is_home === true.
  • test_main_query_during_rest_request_is_not_home: with REST_REQUEST true, the query that is $wp_the_query has is_home === false.

Why these cases

  • They are the two sides of the is_main_query() condition added in [48053]. Each test asserts a precondition on is_main_query() so a setup mistake can't pass silently.
  • REST_REQUEST is a constant, so both tests use @runInSeparateProcess with @preserveGlobalState disabled, as wpIsServingRestRequest.php does.

Verification

  • Removing && $this->is_main_query() makes only the secondary-query test fail.
  • Deleting the whole REST clause makes only the main-query test fail.
  • Tests_Query_ParseQuery passes (19 tests) and PHPCS reports no errors or warnings on the file.

Use of AI Tools

This change was written with Claude Code (Claude Sonnet 5.5), which drafted the tests and ran the verification above.

This PR is for code review only. Please do not merge it from GitHub; commits happen through Trac.

#10 @lancewillett
11 days ago

In 65108:

Tests: Cover home query flags during REST requests.

Verify WP_Query::parse_query() suppresses is_home for the main query during REST requests while allowing secondary queries to retain it. Isolate the tests in separate processes to define REST_REQUEST.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14797

Props irozum.
See #65819, #50213.

Note: See TracTickets for help on using tickets.