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
This commit is contained in:
Boone Gorges 2016-06-15 12:32:25 +00:00
parent 3c34040bf2
commit ade41fc2cf
2 changed files with 38 additions and 2 deletions

View File

@ -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'] ) {

View File

@ -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;
}
}