I'm trying to get category ID of the current archive displayed.

I tried:

// category (can be a parent category)
$current_cat_ID = get_query_var('cat');
print_r ($current_cat_ID);

It doesn't print anything...

  • where in the template is the code located? are there any custom queries before the code?
    – Michael
    Jul 24, 2012 at 14:21
  • will cat always be set regardless of permalink structure, or maybe just category_name in some cases? I can't recall at the moment...
    – Milo
    Jul 24, 2012 at 14:29

3 Answers 3


you can use get_queried_object()

$category = get_queried_object();
echo $category->term_id;
  • 3
    I've been searching for an answer to his for hours. I wish Wordpress had better documentation. Thank you! Feb 24, 2017 at 15:27
  • @Fayaz It will automatically apply to this answer. Mar 1, 2017 at 11:02

A little bit faster than receiving the object and echo the term ID afterwards is to directly use get_queried_object_id().

  • Yeah I agree, strange that no one mentioned this function, they both were available in the same version, wordpress 3.1.0. Dec 21, 2021 at 10:05

If you want to fecth in category page,you can get id of current category by:

$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;

If you want to get category id of any particular category on any page, you can try using below code:

$category_id = get_cat_ID('Category Name');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.