Handling duplicates with wp_insert_term()

I’m using wp_insert_term which works quite well

Now I want some error handler for when using the same name.

$term_id = wp_insert_term( $term, 'wpsc_product_category', $args );
if($term_id) {//my operations here}

where $term = $_POST['categoryTitle'];

I want to display some error when such name already exists.

In wp_insert_term, how to check if this $term_id is duplicate?
I don’t want to continue my operations on already existing terms.

1 Answer
1

if ( is_wp_error($term_id) ) {

  // oops WP_Error obj returned, so the term existed prior
  // echo $term_id->get_error_message();
}

See if that works for you.

Leave a Comment