How do I determine if a category exists by ID?

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?

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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. }