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.
Built from https://develop.svn.wordpress.org/trunk@52822


git-svn-id: http://core.svn.wordpress.org/trunk@52411 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-03-05 15:35:04 +00:00
parent c17ccc5490
commit 4ae0744585
2 changed files with 33 additions and 12 deletions

View File

@ -3563,16 +3563,22 @@ class WP_Query {
if ( $this->is_category || $this->is_tag || $this->is_tax ) {
if ( $this->is_category ) {
if ( $this->get( 'cat' ) ) {
$term = get_term( $this->get( 'cat' ), 'category' );
} elseif ( $this->get( 'category_name' ) ) {
$term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
$cat = $this->get( 'cat' );
$category_name = $this->get( 'category_name' );
if ( $cat ) {
$term = get_term( $cat, 'category' );
} elseif ( $category_name ) {
$term = get_term_by( 'slug', $category_name, 'category' );
}
} elseif ( $this->is_tag ) {
if ( $this->get( 'tag_id' ) ) {
$term = get_term( $this->get( 'tag_id' ), 'post_tag' );
} elseif ( $this->get( 'tag' ) ) {
$term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
$tag_id = $this->get( 'tag_id' );
$tag = $this->get( 'tag' );
if ( $tag_id ) {
$term = get_term( $tag_id, 'post_tag' );
} elseif ( $tag ) {
$term = get_term_by( 'slug', $tag, 'post_tag' );
}
} else {
// For other tax queries, grab the first term from the first clause.
@ -3601,19 +3607,34 @@ class WP_Query {
}
} elseif ( $this->is_post_type_archive ) {
$post_type = $this->get( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$this->queried_object = get_post_type_object( $post_type );
} elseif ( $this->is_posts_page ) {
$page_for_posts = get_option( 'page_for_posts' );
$this->queried_object = get_post( $page_for_posts );
$this->queried_object_id = (int) $this->queried_object->ID;
} elseif ( $this->is_singular && ! empty( $this->post ) ) {
$this->queried_object = $this->post;
$this->queried_object_id = (int) $this->post->ID;
} elseif ( $this->is_author ) {
$this->queried_object_id = (int) $this->get( 'author' );
$author = (int) $this->get( 'author' );
$author_name = $this->get( 'author_name' );
if ( $author ) {
$this->queried_object_id = $author;
} elseif ( $author_name ) {
$user = get_user_by( 'slug', $author_name );
if ( $user ) {
$this->queried_object_id = $user->ID;
}
}
$this->queried_object = get_userdata( $this->queried_object_id );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-52821';
$wp_version = '6.0-alpha-52822';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.