Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 3 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
1  
@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
add comment

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
add comment

protected by toscho Jun 11 '12 at 11:51

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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.