Make WordPress Core


Ignore:
Timestamp:
03/05/2022 03:33:05 PM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.