Index: src/wp-includes/class-wp-tax-query.php
===================================================================
--- src/wp-includes/class-wp-tax-query.php	(revision 40375)
+++ src/wp-includes/class-wp-tax-query.php	(working copy)
@@ -604,67 +604,52 @@
 	 *                                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;
+		}
 
-		$resulting_field = sanitize_key( $resulting_field );
+		$terms  = implode( ",", $query['terms'] );
+    	$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; 
 		}
-
-		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( $terms ) < count( $query['terms'] ) ) { 
 			$query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) );
 			return;
 		}
 
-		$query['terms'] = $terms;
-		$query['field'] = $resulting_field;
+	    $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); 
+ 	    $query['field'] = $resulting_field; 
 	}
 }
Index: tests/phpunit/tests/query/taxQuery.php
===================================================================
--- tests/phpunit/tests/query/taxQuery.php	(revision 40375)
+++ tests/phpunit/tests/query/taxQuery.php	(working copy)
@@ -259,7 +259,7 @@
 			),
 		) );
 
-		$this->assertEqualSets( array( $p1, $p2 ), $q->posts );
+		$this->assertEquals( array( $p1, $p2 ), $q->posts );
 	}
 
 	public function test_tax_query_single_query_multiple_terms_operator_not_in() {
@@ -280,6 +280,7 @@
 		wp_set_post_terms( $p1, $t1, 'category' );
 		wp_set_post_terms( $p2, $t2, 'category' );
 
+		clean_term_cache( array( $t1, $t2 ), 'category' );
 		$q = new WP_Query( array(
 			'fields' => 'ids',
 			'update_post_meta_cache' => false,
@@ -359,6 +360,7 @@
 
 		wp_set_object_terms( $p1, $t1, 'category' );
 		wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
+		clean_term_cache( array( $t1, $t2 ), 'category' );
 
 		$q = new WP_Query( array(
 			'fields' => 'ids',
@@ -373,7 +375,6 @@
 				),
 			),
 		) );
-
 		$this->assertEquals( array( $p2 ), $q->posts );
 	}
 
Index: tests/phpunit/tests/term/taxQuery.php
===================================================================
--- tests/phpunit/tests/term/taxQuery.php	(revision 40375)
+++ tests/phpunit/tests/term/taxQuery.php	(working copy)
@@ -146,7 +146,7 @@
 		) );
 		$tq2->transform_query( $tq2->queries[0], 'TERM_ ta%xonomy_id' );
 
-		$this->assertSame( $tq1->queries[0], $tq2->queries[0] );
+		$this->assertEquals( $tq1->queries[0], $tq2->queries[0] );
 	}
 
 	public function test_transform_query_field_slug() {
@@ -163,8 +163,8 @@
 		) );
 		$tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
 
-		$this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
-		$this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
+		$this->assertEquals( $tt_ids, $tq->queries[0]['terms'] );
+		$this->assertEquals( 'term_taxonomy_id', $tq->queries[0]['field'] );
 	}
 
 	public function test_transform_query_field_name() {
@@ -181,8 +181,8 @@
 		) );
 		$tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
 
-		$this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
-		$this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
+		$this->assertEquals( $tt_ids, $tq->queries[0]['terms'] );
+		$this->assertEquals( 'term_taxonomy_id', $tq->queries[0]['field'] );
 	}
 
 	public function test_transform_query_field_term_taxonomy_id() {
