term_exists returns null, but wp_insert_term can’t insert because it exists
-
I have a problem with the functions term_exists and wp_insert_term. Say I do something like this:
$attributeTerm = "X & Y"; $attribute = "pa_color"; $term = term_exists($attributeTerm, $attribute); WP_CLI::log(var_export($term, 1)); if (!$term) { $term = wp_insert_term($attributeTerm, $attribute); if (is_wp_error($term)) { WP_CLI::log(print_r($term, 1)); } }
If the slug of the term is kind of non-standard, like x-y_895604_895604, then term_exists will return NULL. So the if-block is entered. But then wp_insert_term returns a WP_Error object with the error that the term exists with the same name in that taxonomy.
Why does this happen? And is it future proof if I use the WP_Error object to get the term_id of the existing term?
I’d rather the term_exists function work correctly, though.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘term_exists returns null, but wp_insert_term can’t insert because it exists’ is closed to new replies.