WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted
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.

share|improve this answer
that's exactly what I was looking for, thanks ) – CBeTJlu4ok May 14 '13 at 3:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.