I am creating a category template that uses WP_query to get posts from my custom post type...

$query = new WP_Query( array( 'post_type' => array( 'mycpt' ), 'posts_per_page' => 5 ) );

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<?php the_title(); ?>

<?php endwhile; ?>

How do i get the category id for the current category type? I can get the category title with...

<?php single_cat_title( '', true ); ?>

But how do I get the ID?

1 Answer 1


On a category archive, the "queried object" will have what you need.

$cat = get_queried_object();

You can access it by means of get_query_var():

$cat = get_query_var('cat');

You can also pull is straight from the $wp_query global:


All of these assume that you are actually on a proper category archive page and that nothing has clobbered or otherwise corrupted the main query.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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