WordPress.org

Make WordPress Core

Changeset 36003


Ignore:
Timestamp:
12/18/2015 05:43:46 PM (5 years ago)
Author:
boonebgorges
Message:

Ensure get_terms() results are unique when using 'meta_query'.

The introduction of 'meta_query' to get_terms() in 4.4 made it possible for
get_terms() to erroneously return duplicate results. To address the issue,
we add the DISTINCT keyword to the SQL query when a 'meta_query' parameter
has been provided.

Props @jadpm.
Fixes #35137.

Location:
trunk
Files:
2 edited

Legend:

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

    r35907 r36003  
    13541354    // Meta query support.
    13551355    $join = '';
     1356    $distinct = '';
    13561357    if ( ! empty( $args['meta_query'] ) ) {
    13571358        $mquery = new WP_Meta_Query( $args['meta_query'] );
     
    13601361        $join  .= $mq_sql['join'];
    13611362        $where .= $mq_sql['where'];
     1363        $distinct .= "DISTINCT";
    13621364    }
    13631365
     
    14091411    $join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
    14101412
    1411     $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
     1413    $pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );
    14121414
    14131415    /**
     
    14251427    $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
    14261428    $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
     1429    $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
    14271430    $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
    14281431    $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
    14291432    $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
    14301433
    1431     $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
     1434    $query = "SELECT $distinct $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
    14321435
    14331436    // $args can be anything. Only use the args defined in defaults to compute the key.
  • trunk/tests/phpunit/tests/term/getTerms.php

    r35242 r36003  
    15721572
    15731573    /**
     1574     * @ticket 35137
     1575     */
     1576    public function test_meta_query_should_not_return_duplicates() {
     1577        register_taxonomy( 'wptests_tax', 'post' );
     1578        $terms = self::factory()->term->create_many( 1, array( 'taxonomy' => 'wptests_tax' ) );
     1579        add_term_meta( $terms[0], 'foo', 'bar' );
     1580        add_term_meta( $terms[0], 'foo', 'ber' );
     1581
     1582        $found = get_terms( 'wptests_tax', array(
     1583            'hide_empty' => false,
     1584            'meta_query' => array(
     1585                array(
     1586                    'key' => 'foo',
     1587                    'value' => 'bur',
     1588                    'compare' => '!=',
     1589                ),
     1590            ),
     1591            'fields' => 'ids',
     1592        ) );
     1593
     1594        $this->assertEqualSets( array( $terms[0] ), $found );
     1595    }
     1596
     1597    /**
    15741598     * @ticket 14162
    15751599     */
Note: See TracChangeset for help on using the changeset viewer.