WordPress.org

Make WordPress Core

Changeset 37721


Ignore:
Timestamp:
06/16/2016 02:00:02 AM (3 years ago)
Author:
boonebgorges
Message:

Query: set_found_posts() must run immediately after main query.

If not run immediately after, the SELECT FOUND_ROWS() query might refer to
a different query, such as the one used to populate the post cache for a split
query.

Introduced in [37692].

Fixes #36687.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r37712 r37721  
    36423642                if ( $ids ) {
    36433643                    $this->posts = $ids;
     3644                    $this->set_found_posts( $q, $limits );
    36443645                    _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    36453646                } else {
     
    36483649            } else {
    36493650                $this->posts = $wpdb->get_results( $this->request );
    3650             }
    3651         }
    3652 
    3653         $this->set_found_posts( $q, $limits );
     3651                $this->set_found_posts( $q, $limits );
     3652            }
     3653        }
    36543654
    36553655        // Convert to WP_Post objects.
  • trunk/tests/phpunit/tests/post/query.php

    r37712 r37721  
    448448        return 1;
    449449    }
     450
     451    /**
     452     * @ticket 36687
     453     */
     454    public function test_set_found_posts_fields_ids() {
     455        register_post_type( 'wptests_pt' );
     456
     457        $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
     458
     459        foreach ( $posts as $p ) {
     460            clean_post_cache( $p );
     461        }
     462
     463        $q = new WP_Query( array(
     464            'post_type' => 'wptests_pt',
     465            'posts_per_page' => 1,
     466            'fields' => 'ids',
     467        ) );
     468
     469        $this->assertEquals( 2, $q->found_posts );
     470        $this->assertEquals( 2, $q->max_num_pages );
     471    }
     472
     473    /**
     474     * @ticket 36687
     475     */
     476    public function test_set_found_posts_fields_idparent() {
     477        register_post_type( 'wptests_pt' );
     478
     479        $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
     480        foreach ( $posts as $p ) {
     481            clean_post_cache( $p );
     482        }
     483
     484        $q = new WP_Query( array(
     485            'post_type' => 'wptests_pt',
     486            'posts_per_page' => 1,
     487            'fields' => 'id=>parent',
     488        ) );
     489
     490        $this->assertEquals( 2, $q->found_posts );
     491        $this->assertEquals( 2, $q->max_num_pages );
     492    }
     493
     494    /**
     495     * @ticket 36687
     496     */
     497    public function test_set_found_posts_fields_split_the_query() {
     498        register_post_type( 'wptests_pt' );
     499
     500        $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
     501        foreach ( $posts as $p ) {
     502            clean_post_cache( $p );
     503        }
     504
     505        add_filter( 'split_the_query', '__return_true' );
     506        $q = new WP_Query( array(
     507            'post_type' => 'wptests_pt',
     508            'posts_per_page' => 1,
     509        ) );
     510        remove_filter( 'split_the_query', '__return_true' );
     511
     512        $this->assertEquals( 2, $q->found_posts );
     513        $this->assertEquals( 2, $q->max_num_pages );
     514    }
     515
     516    /**
     517     * @ticket 36687
     518     */
     519    public function test_set_found_posts_fields_not_split_the_query() {
     520        register_post_type( 'wptests_pt' );
     521
     522        $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
     523        foreach ( $posts as $p ) {
     524            clean_post_cache( $p );
     525        }
     526
     527        // ! $split_the_query
     528        add_filter( 'split_the_query', '__return_false' );
     529        $q = new WP_Query( array(
     530            'post_type' => 'wptests_pt',
     531            'posts_per_page' => 1,
     532        ) );
     533        remove_filter( 'split_the_query', '__return_false' );
     534
     535        $this->assertEquals( 2, $q->found_posts );
     536        $this->assertEquals( 2, $q->max_num_pages );
     537    }
    450538}
Note: See TracChangeset for help on using the changeset viewer.