From 1792018b4b914b9e68df5ecafe2071a0ba41f2f7 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 25 Mar 2022 09:54:52 +0000 Subject: [PATCH] Query: Ensure that sticky post query returns all sticky posts. Ensure that the `posts_per_page` parameter submit to sticky post query matches the number of sticky posts requested. Follow-up to [52982] Props Spacedmonkey, peterwilsoncc. See #36907. git-svn-id: https://develop.svn.wordpress.org/trunk@52990 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-query.php | 1 + tests/phpunit/tests/query/stickies.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 10e7a3c7fa..35b29a902d 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3263,6 +3263,7 @@ class WP_Query { 'post__in' => $sticky_posts, 'post_type' => $post_type, 'post_status' => 'publish', + 'posts_per_page' => count( $sticky_posts ), 'suppress_filters' => $q['suppress_filters'], 'cache_results' => $q['cache_results'], 'update_post_meta_cache' => $q['update_post_meta_cache'], diff --git a/tests/phpunit/tests/query/stickies.php b/tests/phpunit/tests/query/stickies.php index 3f02ae32e5..0070f4cb31 100644 --- a/tests/phpunit/tests/query/stickies.php +++ b/tests/phpunit/tests/query/stickies.php @@ -126,4 +126,27 @@ class Tests_Query_Stickies extends WP_UnitTestCase { $this->assertTrue( $sticky_query_vars['ignore_sticky_posts'] ); $this->assertTrue( $sticky_query_vars['no_found_rows'] ); } + + /** + * @ticket 36907 + */ + public function test_stickies_should_limit_query() { + $sticky_count = 6; + $post_date = gmdate( 'Y-m-d H:i:s', time() - 10000 ); + $post_ids = self::factory()->post->create_many( $sticky_count, array( 'post_date' => $post_date ) ); + add_filter( + 'pre_option_sticky_posts', + function () use ( $post_ids ) { + return $post_ids; + } + ); + + $filter = new MockAction(); + add_filter( 'posts_pre_query', array( $filter, 'filter' ), 10, 2 ); + $this->go_to( '/' ); + $filter_args = $filter->get_args(); + $sticky_query_vars = $filter_args[1][1]->query_vars; + + $this->assertSame( $sticky_query_vars['posts_per_page'], $sticky_count ); + } }