Make WordPress Core

Ticket #41796: 41796.diff

File 41796.diff, 3.2 KB (added by boonebgorges, 9 years ago)
  • src/wp-includes/class-wp-term-query.php

    diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php
    index 12e7dec242..a93e67b910 100644
    class WP_Term_Query { 
    551551                        $limits = '';
    552552                }
    553553
     554                $do_distinct = false;
     555
     556                // When querying for a specific number of terms, let MySQL eliminate duplicates.
     557                if ( ! empty( $limits ) && 'all_with_object_id' !== $args['fields'] ) {
     558                        $do_distinct = true;
     559                }
    554560
    555561                if ( ! empty( $args['search'] ) ) {
    556562                        $this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] );
    class WP_Term_Query { 
    568574                if ( ! empty( $meta_clauses ) ) {
    569575                        $join .= $mq_sql['join'];
    570576                        $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] );
    571                         $distinct .= "DISTINCT";
    572 
     577                        $do_distinct = true;
    573578                }
    574579
    575580                $selects = array();
    class WP_Term_Query { 
    631636
    632637                $where = implode( ' AND ', $this->sql_clauses['where'] );
    633638
     639                $distinct = $do_distinct ? 'DISTINCT' : '';
     640
    634641                /**
    635642                 * Filters the terms query SQL clauses.
    636643                 *
  • tests/phpunit/tests/term/query.php

    diff --git tests/phpunit/tests/term/query.php tests/phpunit/tests/term/query.php
    index b2e2a27621..73a4a749cd 100644
    class Tests_Term_Query extends WP_UnitTestCase { 
    427427                $terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) );
    428428                $this->assertEquals( array( $term_ids[1], $term_ids[0], 1 ), wp_list_pluck( $terms, 'term_id' ) );
    429429        }
     430
     431        /**
     432         * @ticket 41796
     433         */
     434        public function test_number_should_work_with_object_ids() {
     435                register_taxonomy( 'wptests_tax', 'post' );
     436
     437                $term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     438                $term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     439
     440                $post_1 = self::factory()->post->create();
     441                $post_2 = self::factory()->post->create();
     442
     443                wp_set_object_terms( $post_1, array( $term_1, $term_2 ), 'wptests_tax' );
     444                wp_set_object_terms( $post_2, array( $term_1 ), 'wptests_tax' );
     445
     446                $q = new WP_Term_Query( array(
     447                        'taxonomy' => 'wptests_tax',
     448                        'object_ids' => array( $post_1, $post_2 ),
     449                        'number' => 2,
     450                ) );
     451
     452                $this->assertEqualSets( array( $term_1, $term_2 ), wp_list_pluck( $q->terms, 'term_id' ) );
     453        }
     454
     455        /**
     456         * @ticket 41796
     457         */
     458        public function test_number_should_work_with_object_ids_and_all_with_object_id() {
     459                register_taxonomy( 'wptests_tax', 'post' );
     460
     461                $term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     462                $term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     463
     464                $post_1 = self::factory()->post->create();
     465                $post_2 = self::factory()->post->create();
     466
     467                wp_set_object_terms( $post_1, array( $term_1, $term_2 ), 'wptests_tax' );
     468                wp_set_object_terms( $post_2, array( $term_1 ), 'wptests_tax' );
     469
     470                $q = new WP_Term_Query( array(
     471                        'taxonomy' => 'wptests_tax',
     472                        'object_ids' => array( $post_1, $post_2 ),
     473                        'fields' => 'all_with_object_id',
     474                        'number' => 2,
     475                ) );
     476
     477                $this->assertEqualSets( array( $term_1, $term_1 ), wp_list_pluck( $q->terms, 'term_id' ) );
     478        }
    430479}