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.

git-svn-id: https://develop.svn.wordpress.org/trunk@52822 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-03-05 15:33:05 +00:00
parent 73fdc6ada2
commit f0fdad1c8e
2 changed files with 55 additions and 11 deletions
+23
View File
@@ -696,4 +696,27 @@ class Tests_Query extends WP_UnitTestCase {
$this->assertSame( 'tax1', get_query_var( 'taxonomy' ) );
$this->assertSame( 'term1', get_query_var( 'term' ) );
}
/**
* @ticket 55100
*/
public function test_get_queried_object_should_work_for_author_name_before_get_posts() {
$user_id = self::factory()->user->create();
$user = get_user_by( 'ID', $user_id );
$post_id = self::factory()->post->create(
array(
'post_author' => $user_id,
)
);
$this->go_to( home_url( '?author=' . $user_id ) );
$this->assertInstanceOf( 'WP_User', get_queried_object() );
$this->assertSame( get_queried_object_id(), $user_id );
$this->go_to( home_url( '?author_name=' . $user->user_nicename ) );
$this->assertInstanceOf( 'WP_User', get_queried_object() );
$this->assertSame( get_queried_object_id(), $user_id );
}
}