diff --git src/wp-includes/class-wp-tax-query.php src/wp-includes/class-wp-tax-query.php
index 9bff196f28..b46aede70a 100644
--- src/wp-includes/class-wp-tax-query.php
+++ src/wp-includes/class-wp-tax-query.php
@@ -623,7 +623,12 @@ class WP_Tax_Query {
 					 * 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' ) ) . "'";
+					$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'] );
diff --git tests/phpunit/tests/query/taxQuery.php tests/phpunit/tests/query/taxQuery.php
index 7582eb2e1a..34ed7fad1f 100644
--- tests/phpunit/tests/query/taxQuery.php
+++ tests/phpunit/tests/query/taxQuery.php
@@ -1380,4 +1380,33 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase {
 
 		_unregister_taxonomy( 'foo' );
 	}
+
+	/**
+	 * @ticket 39315
+	 */
+	public function test_tax_terms_should_not_be_double_escaped() {
+		$name = "Don't worry be happy";
+
+		register_taxonomy( 'wptests_tax', 'post' );
+		$t = self::factory()->term->create( array(
+			'taxonomy' => 'wptests_tax',
+			'name' => $name,
+		) );
+
+		$p = self::factory()->post->create();
+		wp_set_object_terms( $p, array( $t ), 'wptests_tax' );
+
+		$q = new WP_Query( array(
+			'fields' => 'ids',
+			'tax_query' => array(
+				array(
+					'taxonomy' => 'wptests_tax',
+					'field' => 'name',
+					'terms' => $name,
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( $p ), $q->posts );
+	}
 }
