Make WordPress Core

Changeset 41880


Ignore:
Timestamp:
10/16/2017 06:34:29 PM (9 years ago)
Author:
boonebgorges
Message:

Don't force distinct term queries when specifying number and object_ids.

This reverts [41377], which caused performance problems on sites with a large
number of terms.

See #41796.

Location:
trunk
Files:
2 edited

Legend:

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

    r41769 r41880  
    554554                }
    555555
    556                 $do_distinct = false;
    557 
    558                 /*
    559                  * Duplicate terms are generally removed when necessary after the database query.
    560                  * But when a LIMIT clause is included in the query, we let MySQL enforce
    561                  * distinctness so the count is correct.
    562                  */
    563                 if ( ! empty( $limits ) && 'all_with_object_id' !== $args['fields'] ) {
    564                         $do_distinct = true;
    565                 }
    566556
    567557                if ( ! empty( $args['search'] ) ) {
    … …  
    581571                        $join .= $mq_sql['join'];
    582572                        $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] );
    583                         $do_distinct = true;
     573                        $distinct .= "DISTINCT";
     574
    584575                }
    585576
    … …  
    643634                $where = implode( ' AND ', $this->sql_clauses['where'] );
    644635
    645                 $distinct = $do_distinct ? 'DISTINCT' : '';
    646 
    647636                /**
    648637                 * Filters the terms query SQL clauses.
  • trunk/tests/phpunit/tests/term/query.php

    r41809 r41880  
    472472                $this->assertSame( $expected, $found3 );
    473473        }
    474 
    475         /**
    476          * @ticket 41796
    477          */
    478         public function test_number_should_work_with_object_ids() {
    479                 register_taxonomy( 'wptests_tax', 'post' );
    480 
    481                 $term_1 = self::factory()->term->create( array(
    482                         'taxonomy' => 'wptests_tax',
    483                 ) );
    484                 $term_2 = self::factory()->term->create( array(
    485                         'taxonomy' => 'wptests_tax',
    486                 ) );
    487 
    488                 $post_1 = self::factory()->post->create();
    489                 $post_2 = self::factory()->post->create();
    490 
    491                 wp_set_object_terms( $post_1, array( $term_1, $term_2 ), 'wptests_tax' );
    492                 wp_set_object_terms( $post_2, array( $term_1 ), 'wptests_tax' );
    493 
    494                 $q = new WP_Term_Query( array(
    495                         'taxonomy' => 'wptests_tax',
    496                         'object_ids' => array( $post_1, $post_2 ),
    497                         'number' => 2,
    498                 ) );
    499 
    500                 $this->assertEqualSets( array( $term_1, $term_2 ), wp_list_pluck( $q->terms, 'term_id' ) );
    501         }
    502 
    503         /**
    504          * @ticket 41796
    505          */
    506         public function test_number_should_work_with_object_ids_and_all_with_object_id() {
    507                 register_taxonomy( 'wptests_tax', 'post' );
    508 
    509                 $term_1 = self::factory()->term->create( array(
    510                         'taxonomy' => 'wptests_tax',
    511                 ) );
    512                 $term_2 = self::factory()->term->create( array(
    513                         'taxonomy' => 'wptests_tax',
    514                 ) );
    515 
    516                 $post_1 = self::factory()->post->create();
    517                 $post_2 = self::factory()->post->create();
    518 
    519                 wp_set_object_terms( $post_1, array( $term_1, $term_2 ), 'wptests_tax' );
    520                 wp_set_object_terms( $post_2, array( $term_1 ), 'wptests_tax' );
    521 
    522                 $q = new WP_Term_Query( array(
    523                         'taxonomy' => 'wptests_tax',
    524                         'object_ids' => array( $post_1, $post_2 ),
    525                         'fields' => 'all_with_object_id',
    526                         'number' => 2,
    527                 ) );
    528 
    529                 $this->assertEqualSets( array( $term_1, $term_1 ), wp_list_pluck( $q->terms, 'term_id' ) );
    530         }
    531474}
Note: See TracChangeset for help on using the changeset viewer.