From b04671e52f10c971b2a95f5064a03dcede25525d Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 20 Dec 2020 14:35:58 +0000 Subject: [PATCH] Query: Ensure the author archive title always shows the name of the queried author, regardless of whether there are results. This brings the behaviour inline with the `` element of the page which always shows the author name. Props Tkama, subrataemfluence Fixes #44183 git-svn-id: https://develop.svn.wordpress.org/trunk@49843 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 4 +-- tests/phpunit/tests/general/template.php | 33 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 08976d6d1b..9e163ab633 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -597,8 +597,8 @@ class WP { $GLOBALS['single'] = 1; } - if ( $wp_query->is_author() && isset( $wp_query->post ) ) { - $GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author ); + if ( $wp_query->is_author() ) { + $GLOBALS['authordata'] = get_userdata( get_queried_object_id() ); } } diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 096cf8bce8..fd7f9499d3 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -589,4 +589,37 @@ class Tests_General_Template extends WP_UnitTestCase { array( 0, false ), ); } + + /** + * @ticket 44183 + */ + function test_get_the_archive_title_is_correct_for_author_queries() { + $user_with_posts = $this->factory()->user->create_and_get( + array( + 'role' => 'author', + ) + ); + $user_with_no_posts = $this->factory()->user->create_and_get( + array( + 'role' => 'author', + ) + ); + + $this->factory()->post->create( [ + 'post_author' => $user_with_posts->ID, + ] ); + + // Simplify the assertion by removing the default archive title prefix: + add_filter( 'get_the_archive_title_prefix', '__return_empty_string' ); + + $this->go_to( get_author_posts_url( $user_with_posts->ID ) ); + $title_when_posts = get_the_archive_title(); + + $this->go_to( get_author_posts_url( $user_with_no_posts->ID ) ); + $title_when_no_posts = get_the_archive_title(); + + // Ensure the title is correct both when the user has posts and when they dont: + $this->assertSame( $user_with_posts->display_name, $title_when_posts ); + $this->assertSame( $user_with_no_posts->display_name, $title_when_no_posts ); + } }