From 72a264e9c050ebd96790f13a7e3f29cce38e5732 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 18 Dec 2015 17:49:22 +0000 Subject: [PATCH] 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 --- wp-includes/taxonomy.php | 7 +++++-- wp-includes/version.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index f361c3be49..3dcdede818 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 880aabfed6..49a138af4c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.