Make WordPress Core

Ticket #55358: 55358-term_exists-parent-param.patch

File 55358-term_exists-parent-param.patch, 1.7 KB (added by sachinrajcp123, 4 months ago)

Makes term_exists() respect the $parent parameter ✅ Ensures accurate checks for terms under different parents ✅ Prevents false “term already exists” errors in hierarchical taxonomies ✅ Improves reliability for wp_insert_term() and related functions

  • wp-includes/taxonomy.php

    diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php
    a b  
    12341234 */
    12351235function term_exists( $term, $taxonomy = '', $parent = 0 ) {
    12361236    global $wpdb;
    1237 
    1238     if ( is_int( $term ) ) {
    1239         $where = 't.term_id = %d';
    1240         $else_where = 't.term_id = %d';
     1237
     1238    // Ensure parent is an integer for proper comparison.
     1239    $parent = (int) $parent;
     1240
     1241    if ( is_int( $term ) ) {
     1242        $where = 't.term_id = %d';
     1243        $else_where = 't.term_id = %d';
    12411244    } else {
    12421245        $term = trim( wp_unslash( $term ) );
    12431246        $slug = sanitize_title( $term );
    12441247        $where = 't.slug = %s';
    12451248        $else_where = 't.name = %s';
    12461249    }
    12471250
    1248     $query = "SELECT t.term_id FROM $wpdb->terms AS t";
     1251    $query = "SELECT t.term_id FROM $wpdb->terms AS t";
     1252
     1253    // Only join term_taxonomy if taxonomy or parent is being checked.