How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Get the Current Category ID in WordPress
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Get the Current Category ID in WordPress
Web Development

How to Get the Current Category ID in WordPress

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
WordPress get current category ID — three methods by page context
SHARE

Three idiomatic ways to get the wordpress current category id: get_queried_object()->term_id on an archive page, get_the_category()[0]->term_id on a single post, and get_cat_ID('Name') when you only have the category name. Each has a clear “right context” — pick the one that matches where the template is running, and guard against the non-archive case so you don’t hit null.

Contents
  • TL;DR
  • On a category archive page
  • Alternative — get_query_var('cat')
  • On a single post
  • From a name you already have
  • Using the ID once you have it
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WordPress 6.5 and PHP 8.3. Originally published 2023-01-03, rewritten and updated 2026-04-23.

TL;DR

// On a category archive (category.php, archive.php)
$cat_id = get_queried_object()->term_id;

// On a single post (single.php)
$cat_id = get_the_category()[0]->term_id;

// From a name you already have
$cat_id = get_cat_ID( 'News' );

On a category archive page

$queried_object = get_queried_object();
$category_id    = $queried_object->term_id;

get_queried_object() returns whichever object the current page is about: a WP_Term for taxonomy archives, a WP_Post for singular pages, a WP_User for author archives. On a category archive (/category/news/), you get the category’s WP_Term and term_id is the ID you want.

Because the object type varies by page, always guard before drilling in:

$obj = get_queried_object();

if ( $obj instanceof WP_Term ) {
    $category_id = $obj->term_id;
}

The instanceof WP_Term check also catches tag and custom-taxonomy archives, which is usually what you want — the rest of the template can branch on the taxonomy if needed ($obj->taxonomy === 'category').

Alternative — get_query_var('cat')

$category_id = get_query_var( 'cat' );

// Or, if you want the full object
$category = get_category( get_query_var( 'cat' ) );
$cat_id   = $category->cat_ID;

get_query_var('cat') reads the parsed cat from WordPress’s query vars. It only works on the standard category archive and the main query — not on tag archives, custom taxonomies, or secondary WP_Query loops. Reach for it only when you specifically want the category-archive ID and you know the template runs there; otherwise get_queried_object is more robust.

wordpress current category id — get_queried_object for archives, get_the_category for single posts

On a single post

$categories = get_the_category();

if ( ! empty( $categories ) ) {
    $cat_id   = $categories[0]->term_id;
    $cat_name = $categories[0]->name;
}

get_the_category() returns an array of WP_Terms — every category the current post is assigned to. Posts can have multiple categories, so the array index matters. [0] is typically the primary or the first one in the admin’s assigned order.

If your site uses Yoast’s Primary Category feature, respect that selection instead:

$primary_id = get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true );
$cat_id     = $primary_id ?: get_the_category()[0]->term_id;

From a name you already have

$cat_id = get_cat_ID( 'News' );          // exact name match
$term   = get_category_by_slug( 'news' ); // slug match
$cat_id = $term ? $term->term_id : 0;

Useful when you’re displaying a hardcoded category archive widget or filtering by a category you know exists. Both calls are cached after the first hit, so calling them a few times per request is cheap.

Using the ID once you have it

// Fetch posts in the current category
$posts = get_posts( array(
    'cat'            => $cat_id,
    'posts_per_page' => 10,
) );

// Get the category's metadata
$name        = get_cat_name( $cat_id );
$description = category_description( $cat_id );
$link        = get_category_link( $cat_id );

// Edit link (admin)
echo esc_url( get_edit_term_link( $cat_id, 'category' ) );

With the ID in hand, everything downstream — querying, rendering, linking — is a one-liner against WordPress’s taxonomy API.

Frequently asked questions

What’s the cleanest wordpress current category id call on an archive page?

get_queried_object()->term_id. On a category archive, get_queried_object() returns the WP_Term for the requested category, and its term_id is what you want. It works for any taxonomy archive (categories, tags, custom taxonomies) — the same call on a tag archive returns the tag term.

Does get_query_var('cat') always work?

Only on the default category archive and the main query. It reads cat from the parsed query vars, which WordPress only sets for standard category archives — not for tag or custom-taxonomy pages. get_queried_object() is safer because it returns whichever term the page is for, regardless of taxonomy.

What about inside a single-post template (not an archive)?

get_the_category() returns an array of WP_Terms for the current post, and the first one’s term_id is typically the primary category: get_the_category()[0]->term_id. If the theme uses Yoast’s Primary Category feature, use get_post_meta($post_id, '_yoast_wpseo_primary_category', true) to respect that selection.

Can I get the category ID by name without touching the current query?

Yes: get_cat_ID('News'). Pass the human-readable name and it returns the integer ID. For slug lookups, get_category_by_slug('news')->term_id. Both hit the database the first time and cache on subsequent calls, so they’re fine for infrequent use in templates.

Is get_queried_object() safe on a non-archive page?

It returns null or the wrong object type on pages that aren’t taxonomy archives (home page, search results, 404). Always guard: $obj = get_queried_object(); if ($obj instanceof WP_Term) { $cat_id = $obj->term_id; }. The instanceof check is more reliable than a truthy check because it also filters out WP_Post objects (on singular pages) and WP_User (on author archives).

Related guides

  • How to Order Posts by Meta Value in WordPress — the typical thing you do once you’ve grabbed the current category’s posts.
  • How to Apply pre_get_posts on Custom Post Types in WordPress — scoping queries to the current archive.
  • How to Get Posts by Date Range in WordPress — combining date and category filters.
  • How to Check If a User Is Logged In in WordPress — another everyday WP template conditional.

References

WordPress developer reference for get_queried_object, get_the_category, and get_cat_ID: developer.wordpress.org/reference/functions/get_queried_object.

TAGGED:phpwordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article WordPress check if user is logged in with is_user_logged_in() How to Check If a User Is Logged In in WordPress
Next Article WordPress search users by multiple fields — WP_User_Query search_columns + meta_query How to Search Users by Multiple Fields in WordPress
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

Configure WordPress multisite with subdirectories on Nginx — nginx gear + wordpress tree with subsite branches
Web Development

How to Configure WordPress Multisite with Subdirectories on Nginx

12 Min Read
WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter
Web Development

How to Dynamically Change Currency in WooCommerce

7 Min Read
Laravel DataTables custom column search — filterColumn callback handles the search SQL
Web Development

How to Search Custom or Composite Columns in Laravel DataTables

8 Min Read
Laravel rollback specific migration — Artisan migrate:rollback --path command illustration
Web Development

How to Rollback a Specific Migration in Laravel

8 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?