[wp-trac] [WordPress Trac] #55100: `get_queried_object()` doesn't work for `author_name` before `WP_Query::get_posts()`
WordPress Trac
noreply at wordpress.org
Mon Feb 7 04:10:48 UTC 2022
#55100: `get_queried_object()` doesn't work for `author_name` before
`WP_Query::get_posts()`
--------------------------+-----------------------------
Reporter: dd32 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version:
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
`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.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55100>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list