WP_Query::get_queried_object() does not always work in 'pre_get_posts'
|Reported by:||bcworkz||Owned by:|
|Component:||Query||Keywords:||has-patch needs-unit-tests needs-refresh|
Description (last modified by SergeyBiryukov)
The referenced ticket fixes the situation for categories. Inline docs indicate the method returns objects for the following query types: category, tag, taxonomy, posts page, single post, page, author.
Some of these work, some do not. This became apparent from the forums:
Trunk at r27067 used. (and 3.7 for regression check)
Default setup of fresh install except for the following as plugin:
- Hook 'pre_get_posts' and dump get_queried_object return if it is_main_query
- Add custom taxonomy and associate with posts
- Register CPT
Change permalink setting to complete CPT rewrite registration
Also modify the DB as follows to test various query types:
- Add a tag to the Hello World post.
- Add a custom taxonomy term to the Hello World post.
- Add a CPT post
The site was browsed through various pages using the default (none) permalink, then the month and name permalink. The result of getting a queried object was recorded:
|no permalink||month and name|
|uncategorized category||stdClass object||stdClass object|
|taxonomy term archive||stdClass object||stdClass object|
|CPT archive||stdClass object||stdClass object|
|author archive||WP_User object||False|
|posts page||WP_Post object||WP_Post object|
NULLs indicate failure of get_queried_object(). Except for tag archive, all NULLs returned are due to regression prior to 3.7. Why the tag archive failed in 3.8 is unclear, but the proposed patch addresses this issue as well as all other NULL returns.
For those that don't know, the reason for different results with pretty permalinks enabled is different query vars are defined when rewrite rules are applied. The default no pretty permalinks does not use rewrite rules and the query vars can only be what is parsed from the request.
Change History (18)
2 years ago
- Description modified (diff)
- Version changed from trunk to 3.8
2 years ago
- Keywords needs-unit-tests added
- Milestone changed from Awaiting Review to 4.0
- Keywords needs-refresh added
- Milestone changed from 4.0 to Future Release