mirror of
git://develop.git.wordpress.org/
synced 2025-03-21 04:20:01 +01:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@52822 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
73fdc6ada2
commit
f0fdad1c8e
@ -3563,16 +3563,22 @@ class WP_Query {
|
||||
|
||||
if ( $this->is_category || $this->is_tag || $this->is_tax ) {
|
||||
if ( $this->is_category ) {
|
||||
if ( $this->get( 'cat' ) ) {
|
||||
$term = get_term( $this->get( 'cat' ), 'category' );
|
||||
} elseif ( $this->get( 'category_name' ) ) {
|
||||
$term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
|
||||
$cat = $this->get( 'cat' );
|
||||
$category_name = $this->get( 'category_name' );
|
||||
|
||||
if ( $cat ) {
|
||||
$term = get_term( $cat, 'category' );
|
||||
} elseif ( $category_name ) {
|
||||
$term = get_term_by( 'slug', $category_name, 'category' );
|
||||
}
|
||||
} elseif ( $this->is_tag ) {
|
||||
if ( $this->get( 'tag_id' ) ) {
|
||||
$term = get_term( $this->get( 'tag_id' ), 'post_tag' );
|
||||
} elseif ( $this->get( 'tag' ) ) {
|
||||
$term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
|
||||
$tag_id = $this->get( 'tag_id' );
|
||||
$tag = $this->get( 'tag' );
|
||||
|
||||
if ( $tag_id ) {
|
||||
$term = get_term( $tag_id, 'post_tag' );
|
||||
} elseif ( $tag ) {
|
||||
$term = get_term_by( 'slug', $tag, 'post_tag' );
|
||||
}
|
||||
} else {
|
||||
// For other tax queries, grab the first term from the first clause.
|
||||
@ -3601,20 +3607,35 @@ class WP_Query {
|
||||
}
|
||||
} elseif ( $this->is_post_type_archive ) {
|
||||
$post_type = $this->get( 'post_type' );
|
||||
|
||||
if ( is_array( $post_type ) ) {
|
||||
$post_type = reset( $post_type );
|
||||
}
|
||||
|
||||
$this->queried_object = get_post_type_object( $post_type );
|
||||
} elseif ( $this->is_posts_page ) {
|
||||
$page_for_posts = get_option( 'page_for_posts' );
|
||||
$page_for_posts = get_option( 'page_for_posts' );
|
||||
|
||||
$this->queried_object = get_post( $page_for_posts );
|
||||
$this->queried_object_id = (int) $this->queried_object->ID;
|
||||
} elseif ( $this->is_singular && ! empty( $this->post ) ) {
|
||||
$this->queried_object = $this->post;
|
||||
$this->queried_object_id = (int) $this->post->ID;
|
||||
} elseif ( $this->is_author ) {
|
||||
$this->queried_object_id = (int) $this->get( 'author' );
|
||||
$this->queried_object = get_userdata( $this->queried_object_id );
|
||||
$author = (int) $this->get( 'author' );
|
||||
$author_name = $this->get( 'author_name' );
|
||||
|
||||
if ( $author ) {
|
||||
$this->queried_object_id = $author;
|
||||
} elseif ( $author_name ) {
|
||||
$user = get_user_by( 'slug', $author_name );
|
||||
|
||||
if ( $user ) {
|
||||
$this->queried_object_id = $user->ID;
|
||||
}
|
||||
}
|
||||
|
||||
$this->queried_object = get_userdata( $this->queried_object_id );
|
||||
}
|
||||
|
||||
return $this->queried_object;
|
||||
|
@ -696,4 +696,27 @@ class Tests_Query extends WP_UnitTestCase {
|
||||
$this->assertSame( 'tax1', get_query_var( 'taxonomy' ) );
|
||||
$this->assertSame( 'term1', get_query_var( 'term' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55100
|
||||
*/
|
||||
public function test_get_queried_object_should_work_for_author_name_before_get_posts() {
|
||||
$user_id = self::factory()->user->create();
|
||||
$user = get_user_by( 'ID', $user_id );
|
||||
$post_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_author' => $user_id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->go_to( home_url( '?author=' . $user_id ) );
|
||||
|
||||
$this->assertInstanceOf( 'WP_User', get_queried_object() );
|
||||
$this->assertSame( get_queried_object_id(), $user_id );
|
||||
|
||||
$this->go_to( home_url( '?author_name=' . $user->user_nicename ) );
|
||||
|
||||
$this->assertInstanceOf( 'WP_User', get_queried_object() );
|
||||
$this->assertSame( get_queried_object_id(), $user_id );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user