How to get the category ID in WordPress

If you have tinkered around enough with WordPress, there will come a time when you will need to work with Categories (maybe if you want to exclude certain categories from your main page) or display posts from certain categories differently on your blog.

There are a number of template functions in WordPress which take the category ID as input. However, it is not very straight forward to get hold of the category id for a given category from the WordPress admin panel (one way is to dive into the database itself).

Well, there is one easy way which is not so obvious:

Go to your WordPress Admin Panel. Go to Manage. Go to Categories. Basically, you want to get to the page which lists all the categories on your blog (as shown below). Depending on your version of WordPress, there may be a different mechanism to get here.

Now, as you can see, there is no indication here as to what the actual category id of each category is.

However, here is where we need to get a little sneaky, since WordPress offers us a hack into getting the category id of any of these categories.

All you need to do is to click on the category for which you want the category id. That will open the category in edit mode (but you are not here for editing the category). What you need to do is to look at the URL on this new page (where the category is now open for editing). Below is the URL from my blog when I edit one of the categories (notice the highlighted part):

That cat_ID=5 tells you the category ID of the category that you are editing (in this case, it is 5). Simple, isn’t it?

One thought on “How to get the category ID in WordPress”

  1. Hi
    thanks a lot! This post helped me understand why the theme I have installed on my site refused to exclude some posts (I was actually thinking that ID = “blog” for example…doooh)
    Cheers !

Leave a Reply

Your email address will not be published. Required fields are marked *