Symptom
You call wp_insert_term( 'News', 'category', array( 'parent' => 12 ) ) from a plugin, an import script, or a REST callback. It returns a WP_Error with code term_exists and the message “A term with the name provided already exists with this parent.” You open Posts → Categories, filter to parent 12, and see nothing named News. The admin list is empty. The function insists otherwise.
Both observations are correct. They are answers to different questions. The duplicate check inside wp_insert_term() asks whether a term with this name, taxonomy, and parent exists anywhere in the database. The admin screen asks a narrower question shaped by screen options, pagination, and the hide_empty default. When those two answers diverge, the error is authoritative and the screen is not.
Trace: read the error object before you read the database
WP_Error stores a code, one or more messages, and optional data. The term_exists error is constructed with the colliding term’s ID as its data argument. That ID is the single most useful fact in the whole failure, and it is the one most people skip past.
$result = wp_insert_term( 'News', 'category', array( 'parent' => 12 ) );
if ( is_wp_error( $result ) ) {
error_log( 'code: ' . $result->get_error_code() );
error_log( 'message: ' . $result->get_error_message() );
error_log( 'data: ' . var_export( $result->get_error_data(), true ) );
}
Per the WP_Error reference, get_error_data() retrieves the most recently added error data for a code. For term_exists, that data is the existing term’s term_id. Once you have it, you can stop searching the admin UI and start inspecting the row directly.
$term_id = $result->get_error_data();
$term = get_term( (int) $term_id, 'category' );
if ( $term && ! is_wp_error( $term ) ) {
error_log( sprintf(
'id=%d name=%s slug=%s parent=%d count=%d taxonomy=%s',
$term->term_id,
$term->name,
$term->slug,
$term->parent,
$term->count,
$term->taxonomy
) );
}
If get_term() returns a valid object, the term exists. The remaining question is why the admin screen did not show it. If get_term() returns null or a WP_Error, the collision is not a database row at all — see the pre_insert_term section below.
Trace: the duplicate check inside wp_insert_term()
The wp_insert_term() reference documents the contract plainly: “If the term already exists on the same hierarchical level, or the term slug and name are not unique, a WP_Error object will be returned.” The source shows the mechanism. After sanitizing the name and deriving a slug, core runs:
$name_matches = get_terms( array(
'taxonomy' => $taxonomy,
'name' => $name,
'hide_empty' => false,
'parent' => $args['parent'],
'update_term_meta_cache' => false,
) );
Two details matter. First, hide_empty is explicitly false, so the duplicate check sees terms with a post count of zero that the admin list may not display. Second, the query is scoped by parent, so a same-named term under a different parent is not returned by this query — but the subsequent slug check is not scoped the same way.
Core then performs a stricter comparison than get_terms() does, because the name argument does not differentiate accented characters. The source comment is explicit: “The name match in get_terms() doesn’t differentiate accented characters, so we do a stricter comparison here.” The loop compares lowercased names for exact equality and keeps the first match.
If a name match is found, core resolves the slug with get_term_by( 'slug', $slug, $taxonomy ) and then branches on hierarchy:
- Hierarchical taxonomies (categories, custom hierarchical taxonomies): core fetches all siblings under the requested parent and checks whether the name or slug is already present among them. If so, it returns
term_existswith the message “A term with the name provided already exists with this parent.” - Non-hierarchical taxonomies (tags, most custom flat taxonomies): core returns
term_existswith the message “A term with the name provided already exists in this taxonomy.”
Those two messages are not cosmetic. They tell you which branch fired and therefore where to look. The hierarchical message means the collision is at the requested parent level. The flat message means the collision is taxonomy-wide.
Root cause: the collision is real, the visibility is not
The error and the admin screen disagree because they query differently. The duplicate check uses hide_empty => false and an explicit parent. The admin term list is a WP_Term_Query whose defaults, per the WP_Term_Query reference, include 'hide_empty' => true. The admin screen also applies screen options for per-page counts and can be filtered by plugins. Any of the following will produce a term that exists, blocks insertion, and does not appear where you are looking:
- Zero-count term. The term exists with
count = 0. The duplicate check sees it becausehide_emptyis false. The admin list hides it becausehide_emptydefaults to true. This is the most common cause and the easiest to confirm. - Different parent. A term named News exists under parent 7, not parent 12. The
get_terms()name query is scoped to parent 12 and returns nothing, but the slug check viaget_term_by( 'slug', $slug, $taxonomy )is not parent-scoped. If the derived slug matches, the hierarchical branch can still resolve an existing term and return the error. Inspect the returnedterm_id‘sparentbefore assuming the admin list is wrong. - Slug collision across the taxonomy. The name is new, but the slug derived from it (
sanitize_title( $name )) already belongs to a different term in the same taxonomy. The name match may be absent, but the slug path can still produce a collision depending on which branch evaluates.wp_unique_term_slug()normally resolves this by appending a parent slug or a numeric suffix, but the duplicate check runs before that function is called, so a pre-existing slug can short-circuit insertion first. - Admin screen filters. A plugin can filter the term list query on the edit-tags screen. The term is present in the database and visible to
get_terms()withhide_empty => false, but excluded from the rendered list. This is a visibility problem, not a data problem.
None of these is a core defect. The duplicate check is doing what the documentation says it does. The admin screen is doing what its query arguments say it does. The bug is in the assumption that the two views are equivalent.
Root cause: pre_insert_term can fabricate the error
There is a second path to the same error code that involves no database row at all. Before sanitization, wp_insert_term() runs:
$term = apply_filters( 'pre_insert_term', $term, $taxonomy, $args );
if ( is_wp_error( $term ) ) {
return $term;
}
The pre_insert_term reference describes the filter as running “before it is sanitized and inserted into the database.” A plugin can return a WP_Error from this filter with any code and message it likes, including term_exists. If your get_error_data() returns an ID that get_term() cannot resolve, or returns nothing at all, suspect a filter before you suspect the database.
To confirm, temporarily remove filters and retry:
remove_all_filters( 'pre_insert_term' );
$result = wp_insert_term( 'News', 'category', array( 'parent' => 12 ) );
// Restore or re-add your filters afterward.
If the insertion now succeeds, the error was manufactured by a filter. If it still fails, the collision is in the database and you can proceed to the SQL checks below.
Diagnostic sequence
Work in this order. Each step narrows the next.
1. Capture the error data
Log get_error_code(), get_error_message(), and get_error_data() as shown above. The message variant tells you hierarchical versus flat. The data tells you the term ID, if any.
2. Resolve the term ID
Call get_term( $term_id, $taxonomy ) and log name, slug, parent, count, and taxonomy. If the object is valid, the term exists. If it is null, the error came from a filter.
3. Query the database directly
Bypass the object cache and the admin screen. Replace the prefix and taxonomy as needed:
SELECT t.term_id, t.name, t.slug, tt.taxonomy, tt.parent, tt.count
FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'category'
AND ( t.name = 'News' OR t.slug = 'news' );
This returns every row the duplicate check could match, regardless of parent or count. If it returns a row with count = 0, you have confirmed the zero-count case. If it returns a row with a different parent, you have confirmed the parent-scoping case. If it returns nothing, the error is not a database collision.
4. Check for orphaned term_taxonomy rows
A wp_terms row without a matching wp_term_taxonomy row, or a term_taxonomy row pointing at a missing term, can produce confusing results. The join above will not surface a term row with no taxonomy row. Run both directions:
-- Terms with no taxonomy row
SELECT t.term_id, t.name, t.slug
FROM wp_terms AS t
LEFT JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id
WHERE tt.term_id IS NULL;
-- Taxonomy rows with no term row
SELECT tt.term_taxonomy_id, tt.term_id, tt.taxonomy
FROM wp_term_taxonomy AS tt
LEFT JOIN wp_terms AS t ON t.term_id = tt.term_id
WHERE t.term_id IS NULL;
Orphaned rows are not a normal state, but they occur after partial imports, manual SQL edits, or interrupted migrations. If you find one, resolve it before retrying the insertion.
5. Check with WP-CLI
WP-CLI gives you the same view without opening a database client:
wp term list category --name=News --fields=term_id,name,slug,parent,count --format=table
wp term get category <term_id>
wp term list category --slug=news --fields=term_id,name,slug,parent,count
If wp term list returns the term but the admin screen does not, the term exists and the admin query is filtering it out. If wp term list returns nothing, the collision is not a term row and you should return to the pre_insert_term check.
6. Inspect the admin query
If you need to prove the admin screen is filtering the term, hook get_terms_args temporarily and log the arguments for the edit-tags screen. The WP_Term_Query reference documents hide_empty as defaulting to true, and the admin list inherits that default unless a screen option or plugin changes it. A term with count = 0 will not appear under the default. That is the whole explanation in most cases.
Fix
The fix depends on which cause you confirmed. There is no single patch.
- Zero-count term: If the existing term is the one you want, use it. Call
get_term_by( 'name', 'News', 'category' )or use the ID from the error data and assign posts to it. If you genuinely need a second term with the same name under the same parent, you cannot have one — core prevents it by design. Rename one of them or restructure the hierarchy. - Different parent: Decide whether the existing term should move. If so, use
wp_update_term( $term_id, 'category', array( 'parent' => 12 ) ). If not, choose a different name for the new term. The duplicate check is not going to let you create a sibling with the same name under a different parent if the slug path collides. - Slug collision: Pass an explicit
slugargument that does not collide, or letwp_unique_term_slug()do its job by not passing a slug at all. The wp_unique_term_slug() reference documents that it appends a parent slug and then a numeric suffix until the slug is unique. If you are passing a slug manually, you are bypassing that safety net. - pre_insert_term filter: Find the callback. Search your plugin and theme code for
add_filter( 'pre_insert_term'. If it is returning aWP_Errorfor a legitimate reason, handle the error at the call site. If it is returning one incorrectly, fix or remove the filter. - Orphaned rows: Delete the orphaned row after confirming it is not referenced. Back up first. Orphaned rows are a data integrity problem, not a WordPress behavior problem.
Verification
After applying a fix, verify in three places, not one.
- The function call. Retry
wp_insert_term()and confirm it returns an array withterm_idandterm_taxonomy_id, not aWP_Error. Log the return value; do not assume success from the absence of an exception. - The database. Re-run the join query from step 3. Confirm exactly one row matches the name or slug in the target taxonomy, and that its
parentandcountare what you expect. - The admin screen. Reload Posts → Categories. If the term has
count = 0, it may still not appear under the default view. That is expected. Confirm visibility withwp term listor by assigning a post to the term and reloading.
If all three agree, the failure is resolved. If the function succeeds but the admin screen still hides the term, you have a visibility problem, not an insertion problem, and the two should be debugged separately.
FAQ
Why does the error say “with this parent” when the term is under a different parent?
The hierarchical branch fetches siblings under the requested parent and checks names and slugs among them. If the slug check via get_term_by( 'slug', $slug, $taxonomy ) resolves a term elsewhere in the taxonomy, the branch can still return the hierarchical message. The message describes the branch that fired, not necessarily the location of the colliding row. Always inspect the term_id in the error data.
Can a term exist in the database but be invisible to get_term()?
Yes, if the wp_terms row exists without a matching wp_term_taxonomy row. get_term() resolves through the taxonomy table, so a bare term row is not a valid term object. The duplicate check queries through get_terms(), which joins the taxonomy table, so a bare term row should not trigger term_exists on its own. If you suspect orphaned rows, run the left-join queries above.
Does the object cache affect the duplicate check?
wp_insert_term() passes update_term_meta_cache => false to its internal get_terms() call, but term queries can still be served from the object cache depending on the cache backend and the cache_results argument. If you suspect stale cache, flush the term cache for the taxonomy with clean_term_cache( $term_id, $taxonomy ) or use wp cache flush, then retry. Do not treat cache as the first suspect; confirm the database row first.
Is there a way to allow duplicate names?
Core does not provide a supported filter to disable the duplicate-name check. The wp_insert_term_duplicate_term_check filter, documented in the wp_insert_term() reference, runs after insertion and handles a different case: it detects a duplicate created by a race condition and returns the older term’s IDs. It does not bypass the pre-insert name check. If you need duplicate names, you are working against the function’s contract and should reconsider the data model.
Related reading
If you are debugging a fresh install where the front end returns nothing at all, the failure modes are different but the diagnostic discipline is the same: What to Fix First When a New WordPress Site Says Nothing Found.