Skip to content

Commit f9dc276

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@41880 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ae44a09 commit f9dc276

2 files changed

Lines changed: 2 additions & 70 deletions

File tree

src/wp-includes/class-wp-term-query.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,6 @@ public function get_terms() {
553553
$limits = '';
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'] ) ) {
568558
$this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] );
@@ -580,7 +570,8 @@ public function get_terms() {
580570
if ( ! empty( $meta_clauses ) ) {
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

586577
$selects = array();
@@ -642,8 +633,6 @@ public function get_terms() {
642633

643634
$where = implode( ' AND ', $this->sql_clauses['where'] );
644635

645-
$distinct = $do_distinct ? 'DISTINCT' : '';
646-
647636
/**
648637
* Filters the terms query SQL clauses.
649638
*

tests/phpunit/tests/term/query.php

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -471,61 +471,4 @@ public function test_should_allow_same_args_with_the_get_terms() {
471471
$this->assertSame( $expected, $found2 );
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
}

0 commit comments

Comments
 (0)