From 470d448e64051e0b863088cc61ac63ec7e2643de Mon Sep 17 00:00:00 2001 From: Sukhendu Sekhar Guria Date: Fri, 5 Jun 2026 18:14:11 +0530 Subject: [PATCH] perf(query): avoid split query for single post --- src/wp-includes/class-wp-query.php | 3 ++- tests/phpunit/tests/post/query.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index c9bf901ae1576..9b1c0c366e4bf 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3365,7 +3365,8 @@ public function get_posts() { if ( null === $this->posts ) { $split_the_query = ( - $is_unfiltered_query + 1 !== $query_vars['posts_per_page'] + && $is_unfiltered_query && ( wp_using_ext_object_cache() || ( ! empty( $limits ) && $query_vars['posts_per_page'] < 500 ) diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index dece07fbd6b7f..04e786e02468e 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -809,9 +809,39 @@ public function test_split_the_query_object_cache() { ) ); + remove_filter( 'split_the_query', array( $filter, 'filter' ) ); + $this->assertSame( (bool) wp_using_ext_object_cache(), $filter->get_args()[0][0] ); } + /** + * @ticket 57416 + * + * @dataProvider data_split_the_query_should_be_disabled_when_posts_per_page_is_one + */ + public function test_split_the_query_should_be_disabled_when_posts_per_page_is_one( $posts_per_page, $expected ) { + $filter = new MockAction(); + add_filter( 'split_the_query', array( $filter, 'filter' ) ); + + $q = new WP_Query( + array( + 'posts_per_page' => $posts_per_page, + 'cache_results' => false, + ) + ); + + remove_filter( 'split_the_query', array( $filter, 'filter' ) ); + + $this->assertSame( $expected, $filter->get_args()[0][0] ); + } + + public function data_split_the_query_should_be_disabled_when_posts_per_page_is_one() { + return array( + 'one post' => array( 1, false ), + 'two posts' => array( 2, true ), + ); + } + /** * @ticket 56841 */