Index: src/wp-includes/class-wp-term-query.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/class-wp-term-query.php	(revision 44441)
+++ src/wp-includes/class-wp-term-query.php	(date 1546904410000)
@@ -253,6 +253,11 @@
 			$query['child_of'] = false;
 		}
 
+		// If importing, change cache domain, so all calls are uncached.
+		if ( defined( 'WP_IMPORTING' ) ) {
+			$query['cache_domain'] = microtime( true );
+		}
+
 		if ( 'all' == $query['get'] ) {
 			$query['childless']    = false;
 			$query['child_of']     = 0;
Index: src/wp-includes/taxonomy.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/taxonomy.php	(revision 44441)
+++ src/wp-includes/taxonomy.php	(date 1546897012000)
@@ -1354,7 +1354,6 @@
  *
  * @since 3.0.0
  *
- * @global wpdb $wpdb WordPress database abstraction object.
  *
  * @param int|string $term     The term to check. Accepts term ID, slug, or name.
  * @param string     $taxonomy The taxonomy name to use
@@ -1365,56 +1364,62 @@
  *               is specified and the pairing exists.
  */
 function term_exists( $term, $taxonomy = '', $parent = null ) {
-	global $wpdb;
-
-	$select     = "SELECT term_id FROM $wpdb->terms as t WHERE ";
-	$tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";
-
 	if ( is_int( $term ) ) {
 		if ( 0 == $term ) {
 			return 0;
 		}
-		$where = 't.term_id = %d';
+		$_term = get_term( $term, $taxonomy );
+		if ( is_wp_error( $_term ) || is_null( $_term ) ) {
+			return null;
+		}
 		if ( ! empty( $taxonomy ) ) {
-			return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A );
-		} else {
-			return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
+			return array(
+				'term_id'          => (string) $_term->term_id,
+				'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
+			);
 		}
+		return (string) $_term->term_id;
 	}
 
 	$term = trim( wp_unslash( $term ) );
-	$slug = sanitize_title( $term );
+	if ( empty( $term ) ) {
+		return null;
+	}
 
-	$where             = 't.slug = %s';
-	$else_where        = 't.name = %s';
-	$where_fields      = array( $slug );
-	$else_where_fields = array( $term );
-	$orderby           = 'ORDER BY t.term_id ASC';
-	$limit             = 'LIMIT 1';
+	$defaults = array(
+		'get'                    => 'all',
+		'hide_empty'             => false,
+		'number'                 => 1,
+		'update_term_meta_cache' => false,
+		'orderby'                => 'term_id',
+		'suppress_filter'        => true,
+	);
+
+	if ( is_numeric( $parent ) ) {
+		$defaults['parent'] = (int) $parent;
+	}
 	if ( ! empty( $taxonomy ) ) {
-		if ( is_numeric( $parent ) ) {
-			$parent              = (int) $parent;
-			$where_fields[]      = $parent;
-			$else_where_fields[] = $parent;
-			$where              .= ' AND tt.parent = %d';
-			$else_where         .= ' AND tt.parent = %d';
-		}
-
-		$where_fields[]      = $taxonomy;
-		$else_where_fields[] = $taxonomy;
-
-		if ( $result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields ), ARRAY_A ) ) {
-			return $result;
+		$defaults['taxonomy'] = $taxonomy;
+	}
+	$args  = wp_parse_args( array( 'slug' => $term ), $defaults );
+	$terms = get_terms( $args );
+	if ( empty( $terms ) || is_wp_error( $terms ) ) {
+		$args  = wp_parse_args( array( 'name' => $term ), $defaults );
+		$terms = get_terms( $args );
+		if ( empty( $terms ) || is_wp_error( $terms ) ) {
+			return null;
 		}
-
-		return $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields ), ARRAY_A );
 	}
+	$_term = array_shift( $terms );
 
-	if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) {
-		return $result;
+	if ( ! empty( $taxonomy ) ) {
+		return array(
+			'term_id'          => (string) $_term->term_id,
+			'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
+		);
 	}
 
-	return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
+	return (string) $_term->term_id;
 }
 
 /**
Index: tests/phpunit/tests/term/getTerm.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- tests/phpunit/tests/term/getTerm.php	(revision 44441)
+++ tests/phpunit/tests/term/getTerm.php	(date 1546904830000)
@@ -31,6 +31,8 @@
 			array( '%d' )
 		);
 
+		clean_term_cache( $t1['term_id'], 'wptests_tax' );
+
 		return array(
 			array(
 				'term_id'          => $t1['term_id'],
Index: tests/phpunit/tests/term/wpGetObjectTerms.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- tests/phpunit/tests/term/wpGetObjectTerms.php	(revision 44441)
+++ tests/phpunit/tests/term/wpGetObjectTerms.php	(date 1546905683000)
@@ -447,6 +447,9 @@
 		$wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
 		$wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
 
+		// After update, clear caches.
+		clean_term_cache( array( $term_1->term_id, $term_2->term_id, $term_3->term_id ), $this->taxonomy );
+
 		$set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
 
 		$found = wp_get_object_terms(
