From f9dc276353a91c67ba2365e6e8bc472b009a8f90 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 16 Oct 2017 18:34:29 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-term-query.php | 15 +------ tests/phpunit/tests/term/query.php | 57 ------------------------- 2 files changed, 2 insertions(+), 70 deletions(-) diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index be37596031..8e7404e141 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -553,16 +553,6 @@ class WP_Term_Query { $limits = ''; } - $do_distinct = false; - - /* - * Duplicate terms are generally removed when necessary after the database query. - * But when a LIMIT clause is included in the query, we let MySQL enforce - * distinctness so the count is correct. - */ - if ( ! empty( $limits ) && 'all_with_object_id' !== $args['fields'] ) { - $do_distinct = true; - } if ( ! empty( $args['search'] ) ) { $this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] ); @@ -580,7 +570,8 @@ class WP_Term_Query { if ( ! empty( $meta_clauses ) ) { $join .= $mq_sql['join']; $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] ); - $do_distinct = true; + $distinct .= "DISTINCT"; + } $selects = array(); @@ -642,8 +633,6 @@ class WP_Term_Query { $where = implode( ' AND ', $this->sql_clauses['where'] ); - $distinct = $do_distinct ? 'DISTINCT' : ''; - /** * Filters the terms query SQL clauses. * diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 3d3379dfc2..117befadd3 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -471,61 +471,4 @@ class Tests_Term_Query extends WP_UnitTestCase { $this->assertSame( $expected, $found2 ); $this->assertSame( $expected, $found3 ); } - - /** - * @ticket 41796 - */ - public function test_number_should_work_with_object_ids() { - register_taxonomy( 'wptests_tax', 'post' ); - - $term_1 = self::factory()->term->create( array( - 'taxonomy' => 'wptests_tax', - ) ); - $term_2 = self::factory()->term->create( array( - 'taxonomy' => 'wptests_tax', - ) ); - - $post_1 = self::factory()->post->create(); - $post_2 = self::factory()->post->create(); - - wp_set_object_terms( $post_1, array( $term_1, $term_2 ), 'wptests_tax' ); - wp_set_object_terms( $post_2, array( $term_1 ), 'wptests_tax' ); - - $q = new WP_Term_Query( array( - 'taxonomy' => 'wptests_tax', - 'object_ids' => array( $post_1, $post_2 ), - 'number' => 2, - ) ); - - $this->assertEqualSets( array( $term_1, $term_2 ), wp_list_pluck( $q->terms, 'term_id' ) ); - } - - /** - * @ticket 41796 - */ - public function test_number_should_work_with_object_ids_and_all_with_object_id() { - register_taxonomy( 'wptests_tax', 'post' ); - - $term_1 = self::factory()->term->create( array( - 'taxonomy' => 'wptests_tax', - ) ); - $term_2 = self::factory()->term->create( array( - 'taxonomy' => 'wptests_tax', - ) ); - - $post_1 = self::factory()->post->create(); - $post_2 = self::factory()->post->create(); - - wp_set_object_terms( $post_1, array( $term_1, $term_2 ), 'wptests_tax' ); - wp_set_object_terms( $post_2, array( $term_1 ), 'wptests_tax' ); - - $q = new WP_Term_Query( array( - 'taxonomy' => 'wptests_tax', - 'object_ids' => array( $post_1, $post_2 ), - 'fields' => 'all_with_object_id', - 'number' => 2, - ) ); - - $this->assertEqualSets( array( $term_1, $term_1 ), wp_list_pluck( $q->terms, 'term_id' ) ); - } }