diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index c9bf901ae1576..196eca5afd341 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -4047,7 +4047,13 @@ public function get_queried_object() { } } - $this->queried_object = get_userdata( $this->queried_object_id ); + if ( $this->queried_object_id ) { + $user = get_userdata( $this->queried_object_id ); + + if ( $user ) { + $this->queried_object = $user; + } + } } return $this->queried_object; diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 40d23816d1ab6..3a91a48ab9921 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -723,6 +723,40 @@ public function test_get_queried_object_should_work_for_author_name_before_get_p $this->assertSame( get_queried_object_id(), $user_id ); } + /** + * @ticket 65400 + */ + public function test_get_queried_object_should_return_null_when_author_id_is_non_existent() { + add_action( + 'parse_query', + static function ( $query ) { + $query->is_author = true; + $query->set( 'author', 999999 ); + } + ); + + $this->go_to( home_url( '/' ) ); + + $this->assertNull( get_queried_object() ); + } + + /** + * @ticket 65400 + */ + public function test_get_queried_object_should_return_null_when_author_is_unset() { + // Trigger is_author without a valid author query var. + add_action( + 'parse_query', + static function ( $query ) { + $query->is_author = true; + } + ); + + $this->go_to( home_url( '/' ) ); + + $this->assertNull( get_queried_object() ); + } + /** * Tests that the `posts_clauses` filter receives an array of clauses * with the other `posts_*` filters applied, e.g. `posts_join_paged`.