Index: src/wp-includes/class-wp-tax-query.php
===================================================================
--- src/wp-includes/class-wp-tax-query.php	(revision 40844)
+++ src/wp-includes/class-wp-tax-query.php	(working copy)
@@ -571,9 +571,9 @@
 		} elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) {
 			$query = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
 			return;
-		}
+		} 
 
-		$query['terms'] = array_unique( (array) $query['terms'] );
+		$query['terms'] = array_filter( array_unique( (array) $query['terms'] ) );
 
 		if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
 			$this->transform_query( $query, 'term_id' );
@@ -604,67 +604,69 @@
 	 *                                or 'term_id'. Default 'term_id'.
 	 */
 	public function transform_query( &$query, $resulting_field ) {
-		global $wpdb;
-
-		if ( empty( $query['terms'] ) )
+		if ( empty( $query['terms'] ) ) {
 			return;
+		}
 
-		if ( $query['field'] == $resulting_field )
+		if ( $query['field'] == $resulting_field ) {
 			return;
+		}
 
+		$terms           = $query['terms'];
 		$resulting_field = sanitize_key( $resulting_field );
+		$args            = array(
+			'get'                    => 'all',
+			'number'                 => false,
+			'taxonomy'               => $query['taxonomy'],
+			'update_term_meta_cache' => false,
+			'orderby'                => 'none',
+			'suppress_filter'        => true,
+		);
 
 		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'.
-					 */
-					$clean_term = sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' );
-
-					// Match sanitization in wp_insert_term().
-					$clean_term = wp_unslash( $clean_term );
-
-					$term = "'" . esc_sql( $clean_term ) . "'";
-				}
-
-				$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;
+		}
+
+		switch ( $resulting_field ) {
+			case 'term_id':
+				$args['fields'] = 'ids';
+				break;
+			case 'term_taxonomy_id':
+				$args['fields'] = 'tt_ids';
+				break;
 		}
+		$term_query = new WP_Term_Query();
+		$term_list  = $term_query->query( $args );
+
+
+		if ( is_wp_error( $term_list ) ) {
+			$query = $term_list;
 
-		if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
+			return;
+		}
+
+		if ( 'AND' == $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) {
 			$query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) );
+
 			return;
 		}
 
-		$query['terms'] = $terms;
+		if ( ! in_array( $resulting_field, array( 'term_id', 'term_taxonomy_id' ) ) ) {
+			$query['terms'] = wp_list_pluck( $term_list, $resulting_field );
+		} else {
+			$query['terms'] = $term_list;
+		}
+
 		$query['field'] = $resulting_field;
 	}
 }
Index: tests/phpunit/tests/query/taxQuery.php
===================================================================
--- tests/phpunit/tests/query/taxQuery.php	(revision 40844)
+++ tests/phpunit/tests/query/taxQuery.php	(working copy)
@@ -373,7 +373,7 @@
 				),
 			),
 		) );
-
+		
 		$this->assertEquals( array( $p2 ), $q->posts );
 	}
 
