
diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php
--- a/wp-includes/taxonomy.php
+++ b/wp-includes/taxonomy.php
@@ -1234,10 +1234,20 @@
  */
 function term_exists( $term, $taxonomy = '', $parent = 0 ) {
     global $wpdb;
-
-    if ( is_int( $term ) ) {
-        $where = 't.term_id = %d';
-        $else_where = 't.term_id = %d';
+
+    // Ensure parent is an integer for proper comparison.
+    $parent = (int) $parent;
+
+    if ( is_int( $term ) ) {
+        $where = 't.term_id = %d';
+        $else_where = 't.term_id = %d';
     } else {
         $term = trim( wp_unslash( $term ) );
         $slug = sanitize_title( $term );
         $where = 't.slug = %s';
         $else_where = 't.name = %s';
     }
 
-    $query = "SELECT t.term_id FROM $wpdb->terms AS t";
+    $query = "SELECT t.term_id FROM $wpdb->terms AS t";
+
+    // Only join term_taxonomy if taxonomy or parent is being checked.
+    if ( ! empty( $taxonomy ) || $parent > 0 ) {
+        $query .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
+    }
 
     if ( ! empty( $taxonomy ) ) {
         $query .= " WHERE tt.taxonomy = %s AND ( $where OR $else_where )";
-        $prepared = $wpdb->prepare( $query, $taxonomy, $term, $term );
+        $params = array( $taxonomy, $term, $term );
+
+        // Add parent check if specified.
+        if ( $parent > 0 ) {
+            $query .= " AND tt.parent = %d";
+            $params[] = $parent;
+        }
+
+        $prepared = $wpdb->prepare( $query, $params );
     } else {
         $query .= " WHERE $where OR $else_where";
         $prepared = $wpdb->prepare( $query, $term, $term );
     }
 
     $term_id = $wpdb->get_var( $prepared );
 
     if ( $term_id ) {
         return (int) $term_id;
     }
 
     return 0;
 }
