Make WordPress Core


Ignore:
Timestamp:
12/18/2015 05:48:33 PM (9 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.

Merges [36003] to the 4.4 branch.

Props @jadpm.
Fixes #35137.

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/taxonomy.php

    r35725 r36004  
    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.
Note: See TracChangeset for help on using the changeset viewer.