Index: src/wp-includes/class-wp-tax-query.php
===================================================================
--- src/wp-includes/class-wp-tax-query.php	(revision 37684)
+++ src/wp-includes/class-wp-tax-query.php	(working copy)
@@ -600,66 +600,56 @@
 	 * @global wpdb $wpdb The WordPress database abstraction object.
 	 *
 	 * @param array  $query           The single query. Passed by reference.
-	 * @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
+	 * @param string $field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
 	 *                                or 'term_id'. Default 'term_id'.
 	 */
-	public function transform_query( &$query, $resulting_field ) {
-		global $wpdb;
-
-		if ( empty( $query['terms'] ) )
+	public function transform_query( &$query, $field ) {
+		if ( empty( $query['terms'] ) ) {
 			return;
+		}
 
-		if ( $query['field'] == $resulting_field )
+		if ( $query['field'] == $field ) {
 			return;
+		}
 
-		$resulting_field = sanitize_key( $resulting_field );
+		$terms  = implode( ",", $query['terms'] );
+		$count = count( $terms );
+		$args   = array(
+			'taxonomy'               => $query['taxonomy'],
+			'update_term_meta_cache' => false,
+			'hide_empty'             => false,
+			'fields'                 => 'all',
+			'number'                 => $count
+		);
 
 		switch ( $query['field'] ) {
 			case 'slug':
+				$args['slug'] = $terms;
+				break;
 			case 'name':
-				foreach ( $query['terms'] as &$term ) {
-					/*
-					 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
-					 * matter because `sanitize_term_field()` ignores the $term_id param when the
-					 * context is 'db'.
-					 */
-					$term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'";
-				}
-
-				$terms = implode( ",", $query['terms'] );
-
-				$terms = $wpdb->get_col( "
-					SELECT $wpdb->term_taxonomy.$resulting_field
-					FROM $wpdb->term_taxonomy
-					INNER JOIN $wpdb->terms USING (term_id)
-					WHERE taxonomy = '{$query['taxonomy']}'
-					AND $wpdb->terms.{$query['field']} IN ($terms)
-				" );
+				$args['name'] = $terms;
 				break;
 			case 'term_taxonomy_id':
-				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
-				$terms = $wpdb->get_col( "
-					SELECT $resulting_field
-					FROM $wpdb->term_taxonomy
-					WHERE term_taxonomy_id IN ($terms)
-				" );
+				$args['term_taxonomy_id'] = $terms;
 				break;
 			default:
-				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
-				$terms = $wpdb->get_col( "
-					SELECT $resulting_field
-					FROM $wpdb->term_taxonomy
-					WHERE taxonomy = '{$query['taxonomy']}'
-					AND term_id IN ($terms)
-				" );
+				$args['include'] = $terms;
 		}
 
-		if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
+		$term_list = get_terms( $args );
+
+		if ( is_wp_error( $term_list ) ) {
+			$query = $term_list;
+			return;
+		}
+
+		if ( 'AND' == $query['operator'] && count( $term_list ) < $count ) {
 			$query = new WP_Error( 'Inexistent terms' );
 			return;
 		}
 
-		$query['terms'] = $terms;
-		$query['field'] = $resulting_field;
+
+		$query['terms'] = wp_list_pluck( $term_list, $field );
+		$query['field'] = $field;
 	}
 }
