How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get the Current Category ID in WordPress
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > 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.
[mc4wp_form]
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
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

WordPress custom avatar without a plugin — media uploader writes user meta, get_avatar filter renders the image
Web Development

How to Change a User Profile Picture in WordPress Without a Plugin

10 Min Read
Get the first character of a JavaScript string
Web Development

How to Get the First Character of a String in JavaScript

5 Min Read
Laravel Eloquent delete record — trash-bin icon next to User::destroy code snippet
Web Development

How to Delete a Record with Laravel Eloquent (4 Methods)

6 Min Read
GitHub Actions workflow deploying Laravel to a VPS, zero-downtime symlink swap
Web Development

How to Deploy a Laravel App to a VPS with GitHub Actions (Zero-Downtime, No Forge)

11 Min Read
How7o

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

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?