Index: tests/phpunit/tests/query/taxQuery.php
===================================================================
--- tests/phpunit/tests/query/taxQuery.php	(revision 44476)
+++ tests/phpunit/tests/query/taxQuery.php	(working copy)
@@ -434,6 +434,79 @@
 	}
 
 	/**
+	 * @ticket 39140
+	 */
+	public function test_tax_query_single_query_multiple_hierarchical_terms_operator_and() {
+		$europe = self::factory()->term->create(
+			array(
+				'taxonomy' => 'category',
+				'name'     => 'Europe',
+			)
+		);
+		$spain = self::factory()->term->create(
+			array(
+				'taxonomy' => 'category',
+				'name'     => 'Spain',
+				'parent'   => $europe,
+			)
+		);
+		$south_america = self::factory()->term->create(
+			array(
+				'taxonomy' => 'category',
+				'name'     => 'South America',
+			)
+		);
+		$argentina = self::factory()->term->create(
+			array(
+				'taxonomy' => 'category',
+				'name'     => 'Argentina',
+				'parent'   => $south_america,
+			)
+		);
+		$post_a = self::factory()->post->create( array( 'post_category' => array( $argentina ) ) );
+		$post_b = self::factory()->post->create( array( 'post_category' => array( $spain ) ) );
+		$post_c = self::factory()->post->create( array( 'post_category' => array( $argentina, $spain ) ) );
+		$post_d = self::factory()->post->create( array( 'post_category' => array( $europe, $south_america ) ) );
+
+		$posts = get_posts(
+			array(
+				'fields'                 => 'ids',
+				'update_post_meta_cache' => false,
+				'update_post_term_cache' => false,
+				'tax_query'              => array(
+					array(
+						'taxonomy' => 'category',
+						'field'    => 'id',
+						'terms'    => array( $argentina, $spain ),
+						'operator' => 'AND'
+					),
+				),
+			)
+		);
+
+		$this->assertEquals( 1, count( $posts ) );
+
+		$posts = get_posts(
+			array(
+				'fields'                 => 'ids',
+				'update_post_meta_cache' => false,
+				'update_post_term_cache' => false,
+				'tax_query'              => array(
+					array(
+						'taxonomy' => 'category',
+						'field'    => 'id',
+						'terms'    => array( $europe, $south_america ),
+						'operator' => 'AND'
+					),
+				),
+			)
+		);
+
+		$this->assertEquals( 2, count( $posts ) );
+
+	}
+
+	/**
 	 * @ticket 29181
 	 */
 	public function test_tax_query_operator_not_exists() {
