diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 975d1318a5..d30bc43f34 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -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,20 +3607,35 @@ 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' ); + $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' ); - $this->queried_object = get_userdata( $this->queried_object_id ); + $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 ); } return $this->queried_object; diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 8dfd3fb200..c0ce7357a4 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -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 ); + } }