From 839c7c024caaed8f0e77cf2036785c48639f830f Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:36:43 +0200 Subject: [PATCH 1/2] Query: Honor constraining query vars when fetching sticky posts for injection. See #63360. --- src/wp-includes/class-wp-query.php | 55 +++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index c5e048efe3806..0d6f977287cf8 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3628,20 +3628,51 @@ public function get_posts() { // Fetch sticky posts that weren't in the query results. if ( ! empty( $sticky_posts ) ) { - $stickies = get_posts( - array( - 'post__in' => $sticky_posts, - 'post_type' => $post_type, - 'post_status' => 'publish', - 'posts_per_page' => count( $sticky_posts ), - 'suppress_filters' => $query_vars['suppress_filters'], - 'cache_results' => $query_vars['cache_results'], - 'update_post_meta_cache' => $query_vars['update_post_meta_cache'], - 'update_post_term_cache' => $query_vars['update_post_term_cache'], - 'lazy_load_term_meta' => $query_vars['lazy_load_term_meta'], - ) + $sticky_query_args = array( + 'post__in' => $sticky_posts, + 'post_type' => $post_type, + 'post_status' => 'publish', + 'posts_per_page' => count( $sticky_posts ), + 'suppress_filters' => $query_vars['suppress_filters'], + 'cache_results' => $query_vars['cache_results'], + 'update_post_meta_cache' => $query_vars['update_post_meta_cache'], + 'update_post_term_cache' => $query_vars['update_post_term_cache'], + 'lazy_load_term_meta' => $query_vars['lazy_load_term_meta'], + ); + + /* + * Honor constraining query vars from the current query when fetching + * sticky posts, so that stickies unrelated to the requested author, + * slug, term, or other explicit filters are not prepended to the + * results. + */ + $constraining_query_vars = array( + 'author__in', + 'author__not_in', + 'post_name__in', + 'category__in', + 'category__not_in', + 'category__and', + 'tag__in', + 'tag__not_in', + 'tag__and', + 'tag_slug__in', + 'tag_slug__and', + 'post_parent', + 'post_parent__in', + 'post_parent__not_in', + 'tax_query', + 'meta_query', ); + foreach ( $constraining_query_vars as $constraining_query_var ) { + if ( ! empty( $query_vars[ $constraining_query_var ] ) ) { + $sticky_query_args[ $constraining_query_var ] = $query_vars[ $constraining_query_var ]; + } + } + + $stickies = get_posts( $sticky_query_args ); + foreach ( $stickies as $sticky_post ) { array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); ++$sticky_offset; From 6808692a42f26034843e57a7a2be538814e7a6da Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:38:39 +0200 Subject: [PATCH 2/2] Query: Add tests for sticky injection honoring author__in, post_name__in and category__not_in. See #63360. --- tests/phpunit/tests/query/stickies.php | 110 +++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/tests/phpunit/tests/query/stickies.php b/tests/phpunit/tests/query/stickies.php index 4994b0ff1a600..a15729d2ee15c 100644 --- a/tests/phpunit/tests/query/stickies.php +++ b/tests/phpunit/tests/query/stickies.php @@ -157,4 +157,114 @@ static function () use ( $post_ids ) { $this->assertSame( $sticky_query_vars['posts_per_page'], $sticky_count ); } + + /** + * Stickies fetched for injection should honor the query's author__in constraint: + * stickies by other authors must not be prepended, while stickies matching the + * constraint are still moved to the top. + * + * @ticket 63360 + */ + public function test_stickies_should_obey_author__in() { + $author_1 = self::factory()->user->create( array( 'role' => 'author' ) ); + $author_2 = self::factory()->user->create( array( 'role' => 'author' ) ); + + $now = time(); + + $sticky_by_author_1 = self::factory()->post->create( + array( + 'post_author' => $author_1, + 'post_date' => gmdate( 'Y-m-d H:i:s', $now - 20 ), + ) + ); + $plain_by_author_1 = self::factory()->post->create( + array( + 'post_author' => $author_1, + 'post_date' => gmdate( 'Y-m-d H:i:s', $now - 10 ), + ) + ); + $sticky_by_author_2 = self::factory()->post->create( + array( + 'post_author' => $author_2, + 'post_date' => gmdate( 'Y-m-d H:i:s', $now ), + ) + ); + + stick_post( $sticky_by_author_1 ); + stick_post( $sticky_by_author_2 ); + + $q = new WP_Query( + array( + 'author__in' => array( $author_1 ), + ) + ); + + unstick_post( $sticky_by_author_1 ); + unstick_post( $sticky_by_author_2 ); + + $this->assertSame( + array( $sticky_by_author_1, $plain_by_author_1 ), + wp_list_pluck( $q->posts, 'ID' ) + ); + } + + /** + * Stickies fetched for injection should honor the query's post_name__in + * constraint: querying specific slugs must not prepend unrelated stickies. + * + * @ticket 63360 + */ + public function test_stickies_should_obey_post_name__in() { + $post_id = self::factory()->post->create( + array( + 'post_name' => 'not-sticky-1', + ) + ); + + $sticky_id = self::factory()->post->create( + array( + 'post_name' => 'sticky-unrelated', + ) + ); + + stick_post( $sticky_id ); + + $q = new WP_Query( + array( + 'post_name__in' => array( 'not-sticky-1' ), + ) + ); + + unstick_post( $sticky_id ); + + $this->assertSame( array( $post_id ), wp_list_pluck( $q->posts, 'ID' ) ); + } + + /** + * Stickies fetched for injection should honor the query's category__not_in + * constraint: stickies assigned to an excluded term must not be prepended. + * + * @ticket 63360 + */ + public function test_stickies_should_obey_category__not_in() { + $category_id = self::factory()->category->create(); + + $sticky_in_excluded_category = self::factory()->post->create( + array( + 'post_category' => array( $category_id ), + ) + ); + + stick_post( $sticky_in_excluded_category ); + + $q = new WP_Query( + array( + 'category__not_in' => array( $category_id ), + ) + ); + + unstick_post( $sticky_in_excluded_category ); + + $this->assertNotContains( $sticky_in_excluded_category, wp_list_pluck( $q->posts, 'ID' ) ); + } }