#55100 closed defect (bug) (fixed)
`get_queried_object()` doesn't work for `author_name` before `WP_Query::get_posts()`
Reported by: | dd32 | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.0 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
WP_Query->get_queried_object()
fails to return Author data when the query is using author_name
(as with /author/../
permalinks) before WP_Query->get_posts();
is run.
Usually this isn't a problem, as most uses of WP_Query
query for posts when initialised, and when it's not, it's not common that get_queried_object()
runs before the posts are queried.
This not-often use-case appears to be the case with Block Themes though, as https://wordpress.org/news-test/author/hlashbrooke/
fails to display the Author name in the header, but https://wordpress.org/news-test/?author=9355949
works as expected.
In both cases the query is fine, it's just that the queried object isn't available before the posts loop when author_name
is used instead of author
, I'm assuming there's some just-in-time query logic.
The logic that sets the author
field when author_name
is buried inside WP_Query::get_posts
:
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/class-wp-query.php#L2279-L2282
The attached PR shows this and provides a solution in get_queried_object()
similar to how Taxonomy Slugs are handled, by not only relying upon the author
field (but using it if present) and falling back to author_name
instead.
Change History (5)
This ticket was mentioned in PR #2287 on WordPress/wordpress-develop by dd32.
3 years ago
#1
- Keywords has-patch has-unit-tests added
#2
@
3 years ago
Just noting that https://github.com/WordPress/wporg-news-2021/pull/292 is a workaround to go into the theme until this is fixed in core, as a result, the above example URLs may start to visually work.
#4
@
3 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 52822:
SergeyBiryukov commented on PR #2287:
3 years ago
#5
Thanks for the PR! Merged in https://core.trac.wordpress.org/changeset/52822.
See https://core.trac.wordpress.org/ticket/55100