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 know I can get a categories ID by calling get_cat_ID('category-slug'), however, what is the method to call to determine if a category exists by ID when you don't know the slug?

In other words, I need to determine if category id 1 exists. What's the function for this?

Can I just use if(get_category(1)) {//do something?}

share|improve this question
up vote 5 down vote accepted

There is category_exists() function, but it seems to be for internal use in admin so you can try term_exists() instead.

share|improve this answer
you learn new stuff every day. – Bainternet Feb 7 '11 at 14:41
I only need to call it inside of an is_admin() call anyway, in order to set a static variable. Should I use category_exists or term_exists? What if uncategorized (defaults to term_id 1) has been deleted? Does that mean term_exists(1) will always return false? or can term_id 1 be then used for another term item? – Scott B Feb 7 '11 at 16:28
@Scott B category_exists() pretty much only calls term_exists() with specifying category as taxonomy - not much difference which to use. Not sure about reusing IDs, likely they aren't but didn't test it. – Rarst Feb 7 '11 at 16:35

take a look at get_term_by

you can define by what you want to look for the term using slug,ID or name so you can call it like this in your case:

    function check_category_exists($catid){
       $cat_to_check = get_term_by( 'id', $catid, 'category')
       if ($cat_to_check){

       return true;
       else{return false;}

//then call it like this:
if (check_category_exists(1)){  //category exsits. }
share|improve this answer

protected by toscho Jun 11 '12 at 11:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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