Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#55100 closed defect (bug) (fixed)

`get_queried_object()` doesn't work for `author_name` before `WP_Query::get_posts()`

Reported by: dd32's profile dd32 Owned by: sergeybiryukov's profile 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 @dd32
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.

#3 @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 6.0

#4 @SergeyBiryukov
3 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 52822:

Query: Make sure WP_Query::get_queried_object() works for author_name before ::get_posts() is run.

Previously, the queried object with author data was not available before the posts loop when author_name is used in the query instead of author. With block themes, this use case appears to be more common to display the author name in the header.

This commit adjusts the logic in WP_Query::get_queried_object() to fall back to the author_name field if author is not present, similar to how taxonomy slugs are handled.

Follow-up to [1728], [3290], [10992].

Props dd32, swissspidy, SergeyBiryukov.
Fixes #55100.

Note: See TracTickets for help on using tickets.