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.

Built from https://develop.svn.wordpress.org/branches/4.4@36004


git-svn-id: http://core.svn.wordpress.org/branches/4.4@35969 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-12-18 17:49:22 +00:00
parent 6c1286390f
commit 72a264e9c0
2 changed files with 6 additions and 3 deletions

View File

@ -1353,12 +1353,14 @@ function get_terms( $taxonomies, $args = '' ) {
// Meta query support.
$join = '';
$distinct = '';
if ( ! empty( $args['meta_query'] ) ) {
$mquery = new WP_Meta_Query( $args['meta_query'] );
$mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
$join .= $mq_sql['join'];
$where .= $mq_sql['where'];
$distinct .= "DISTINCT";
}
$selects = array();
@ -1408,7 +1410,7 @@ function get_terms( $taxonomies, $args = '' ) {
$join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
$pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );
/**
* Filter the terms query SQL clauses.
@ -1424,11 +1426,12 @@ function get_terms( $taxonomies, $args = '' ) {
$fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
$join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
$where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
$distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
$orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
$order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
$limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
$query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
$query = "SELECT $distinct $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
// $args can be anything. Only use the args defined in defaults to compute the key.
$key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $query );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.4.1-alpha-35945';
$wp_version = '4.4.1-alpha-36004';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.