From ade41fc2cfb8e21c71ad318770bfc772bf9e342d Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 15 Jun 2016 12:32:25 +0000 Subject: [PATCH] Query: After [37692], don't skip `set_found_posts()` when no posts are found. The 'found_posts' filter must continue to run for plugins manipulating post results via filter. Props dd32. Fixes #36687. git-svn-id: https://develop.svn.wordpress.org/trunk@37712 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 5 +++-- tests/phpunit/tests/post/query.php | 35 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 8ec85b2127..ba4d335d74 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3650,10 +3650,11 @@ class WP_Query { } } - // Convert to WP_Post objects and set the found-post totals. + $this->set_found_posts( $q, $limits ); + + // Convert to WP_Post objects. if ( $this->posts ) { $this->posts = array_map( 'get_post', $this->posts ); - $this->set_found_posts( $q, $limits ); } if ( ! $q['suppress_filters'] ) { diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index 551dc1ea35..e5082026e6 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -412,4 +412,39 @@ class Tests_Post_Query extends WP_UnitTestCase { public static function filter_posts_pre_query( $posts ) { return array( 12345 ); } + + /** + * @ticket 36687 + */ + public function test_posts_pre_query_filter_should_respect_set_found_posts() { + global $wpdb; + + $this->post_id = self::factory()->post->create(); + + // Prevent the DB query + add_filter( 'posts_request', '__return_empty_string' ); + add_filter( 'found_posts_query', '__return_empty_string' ); + + // Add the post and found_posts + add_filter( 'the_posts', array( $this, 'filter_the_posts' ) ); + add_filter( 'found_posts', array( $this, 'filter_found_posts' ) ); + + $q = new WP_Query( array( 'suppress_filters' => false ) ); + + remove_filter( 'posts_request', '__return_empty_string' ); + remove_filter( 'found_posts_query', '__return_empty_string' ); + remove_filter( 'the_posts', array( $this, 'filter_the_posts' ) ); + remove_filter( 'found_posts', array( $this, 'filter_found_posts' ) ); + + $this->assertSame( array( $this->post_id ), wp_list_pluck( $q->posts, 'ID' ) ); + $this->assertSame( 1, $q->found_posts ); + } + + public function filter_the_posts() { + return array( get_post( $this->post_id ) ); + } + + public function filter_found_posts( $posts ) { + return 1; + } }