WordPress.org

Make WordPress Core

Changeset 49843


Ignore:
Timestamp:
12/20/2020 02:35:58 PM (10 months ago)
Author:
johnbillion
Message:

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 <title> element of the page which always shows the author name.

Props Tkama, subrataemfluence

Fixes #44183

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r49095 r49843  
    598598        }
    599599
    600         if ( $wp_query->is_author() && isset( $wp_query->post ) ) {
    601             $GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author );
     600        if ( $wp_query->is_author() ) {
     601            $GLOBALS['authordata'] = get_userdata( get_queried_object_id() );
    602602        }
    603603    }
  • trunk/tests/phpunit/tests/general/template.php

    r48937 r49843  
    590590        );
    591591    }
     592
     593    /**
     594     * @ticket 44183
     595     */
     596    function test_get_the_archive_title_is_correct_for_author_queries() {
     597        $user_with_posts = $this->factory()->user->create_and_get(
     598            array(
     599                'role' => 'author',
     600            )
     601        );
     602        $user_with_no_posts = $this->factory()->user->create_and_get(
     603            array(
     604                'role' => 'author',
     605            )
     606        );
     607
     608        $this->factory()->post->create( [
     609            'post_author' => $user_with_posts->ID,
     610        ] );
     611
     612        // Simplify the assertion by removing the default archive title prefix:
     613        add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
     614
     615        $this->go_to( get_author_posts_url( $user_with_posts->ID ) );
     616        $title_when_posts = get_the_archive_title();
     617
     618        $this->go_to( get_author_posts_url( $user_with_no_posts->ID ) );
     619        $title_when_no_posts = get_the_archive_title();
     620
     621        // Ensure the title is correct both when the user has posts and when they dont:
     622        $this->assertSame( $user_with_posts->display_name, $title_when_posts );
     623        $this->assertSame( $user_with_no_posts->display_name, $title_when_no_posts );
     624    }
    592625}
Note: See TracChangeset for help on using the changeset viewer.