Make WordPress Core

Changeset 52822


Ignore:
Timestamp:
03/05/2022 03:33:05 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r52332 r52822  
    35643564        if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    35653565            if ( $this->is_category ) {
    3566                 if ( $this->get( 'cat' ) ) {
    3567                     $term = get_term( $this->get( 'cat' ), 'category' );
    3568                 } elseif ( $this->get( 'category_name' ) ) {
    3569                     $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
     3566                $cat           = $this->get( 'cat' );
     3567                $category_name = $this->get( 'category_name' );
     3568
     3569                if ( $cat ) {
     3570                    $term = get_term( $cat, 'category' );
     3571                } elseif ( $category_name ) {
     3572                    $term = get_term_by( 'slug', $category_name, 'category' );
    35703573                }
    35713574            } elseif ( $this->is_tag ) {
    3572                 if ( $this->get( 'tag_id' ) ) {
    3573                     $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
    3574                 } elseif ( $this->get( 'tag' ) ) {
    3575                     $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
     3575                $tag_id = $this->get( 'tag_id' );
     3576                $tag    = $this->get( 'tag' );
     3577
     3578                if ( $tag_id ) {
     3579                    $term = get_term( $tag_id, 'post_tag' );
     3580                } elseif ( $tag ) {
     3581                    $term = get_term_by( 'slug', $tag, 'post_tag' );
    35763582                }
    35773583            } else {
     
    36023608        } elseif ( $this->is_post_type_archive ) {
    36033609            $post_type = $this->get( 'post_type' );
     3610
    36043611            if ( is_array( $post_type ) ) {
    36053612                $post_type = reset( $post_type );
    36063613            }
     3614
    36073615            $this->queried_object = get_post_type_object( $post_type );
    36083616        } elseif ( $this->is_posts_page ) {
    3609             $page_for_posts          = get_option( 'page_for_posts' );
     3617            $page_for_posts = get_option( 'page_for_posts' );
     3618
    36103619            $this->queried_object    = get_post( $page_for_posts );
    36113620            $this->queried_object_id = (int) $this->queried_object->ID;
     
    36143623            $this->queried_object_id = (int) $this->post->ID;
    36153624        } elseif ( $this->is_author ) {
    3616             $this->queried_object_id = (int) $this->get( 'author' );
    3617             $this->queried_object    = get_userdata( $this->queried_object_id );
     3625            $author      = (int) $this->get( 'author' );
     3626            $author_name = $this->get( 'author_name' );
     3627
     3628            if ( $author ) {
     3629                $this->queried_object_id = $author;
     3630            } elseif ( $author_name ) {
     3631                $user = get_user_by( 'slug', $author_name );
     3632
     3633                if ( $user ) {
     3634                    $this->queried_object_id = $user->ID;
     3635                }
     3636            }
     3637
     3638            $this->queried_object = get_userdata( $this->queried_object_id );
    36183639        }
    36193640
  • trunk/tests/phpunit/tests/query.php

    r52010 r52822  
    697697        $this->assertSame( 'term1', get_query_var( 'term' ) );
    698698    }
     699
     700    /**
     701     * @ticket 55100
     702     */
     703    public function test_get_queried_object_should_work_for_author_name_before_get_posts() {
     704        $user_id = self::factory()->user->create();
     705        $user    = get_user_by( 'ID', $user_id );
     706        $post_id = self::factory()->post->create(
     707            array(
     708                'post_author' => $user_id,
     709            )
     710        );
     711
     712        $this->go_to( home_url( '?author=' . $user_id ) );
     713
     714        $this->assertInstanceOf( 'WP_User', get_queried_object() );
     715        $this->assertSame( get_queried_object_id(), $user_id );
     716
     717        $this->go_to( home_url( '?author_name=' . $user->user_nicename ) );
     718
     719        $this->assertInstanceOf( 'WP_User', get_queried_object() );
     720        $this->assertSame( get_queried_object_id(), $user_id );
     721    }
    699722}
Note: See TracChangeset for help on using the changeset viewer.