Wednesday, May 7, 2025
•3 min read
How to Get the Category of a Post by ID in WordPress Without Plugins
Sometimes you need to display a post’s category outside the main loop or in a custom function. WordPress makes this easy with its built-in `get_the_category()` function. In this quick guide, you’ll learn how to get the category of any post by its ID — no plugins required.
Step 1: Open Your functions.php File
Start by opening your theme’s `functions.php` file, located in your current theme directory (usually `wp-content/themes/your-theme/functions.php`). This is where we’ll define our custom helper function.
Step 2: Create the Function
Now, we’ll define a function that takes a post ID and returns the name of its first associated category. Add this code to your `functions.php` file:
1function get_post_category_by_id( $post_id ) {
2 // Get all categories for the post
3 $categories = get_the_category( $post_id );
4
5 // Check if categories exist
6 if ( ! empty( $categories ) ) {
7 // Return the first category name
8 return $categories[0]->name;
9 }
10
11 // Return false if no categories found
12 return false;
13 }This function uses `get_the_category()` to retrieve all categories linked to the post. It then returns the first category name if one exists, or `false` if no categories are found. You can easily modify this to return all categories or specific details like category IDs or slugs.
Step 3: Use the Function in Your Template
Now that the function is ready, you can call it anywhere in your theme. Just pass the post ID as a parameter:
1$post_id = 123; // Replace with your post ID
2 $category_name = get_post_category_by_id( $post_id );
3
4 if ( $category_name ) {
5 echo 'The category of the post is: ' . esc_html( $category_name );
6 } else {
7 echo 'No categories found for this post.';
8 }This example displays the category name directly on the page. Replace `123` with a real post ID or use it dynamically inside a loop (for example, `$post->ID`).
Complete Example
1<?php
2 // Define the function
3 function get_post_category_by_id( $post_id ) {
4 $categories = get_the_category( $post_id );
5
6 if ( ! empty( $categories ) ) {
7 return $categories[0]->name;
8 }
9
10 return false;
11 }
12
13 // Use it in your theme
14 $post_id = 123; // Replace with your post ID
15 $category_name = get_post_category_by_id( $post_id );
16
17 if ( $category_name ) {
18 echo 'The category of the post is: ' . esc_html( $category_name );
19 } else {
20 echo 'No categories found for this post.';
21 }You can place this code in your theme templates or inside a shortcode for flexible display. To show multiple categories, loop through the `$categories` array instead of returning only the first element.
Useful Links
WordPress Developer Reference – get_the_category():
And that’s all there is to it! You now know how to retrieve a post’s category by ID without using any plugins. Whether you’re building a custom post display or tweaking a theme layout, this snippet will come in handy time and again. Keep coding and experimenting!

I’m an experienced SEO professional with over a decade of helping over 100 businesses rank higher online, especially local businesses, e-commerce stores and SaaS. As the co-founder of LPagery, I specialize in practical, proven strategies for regular SEO and Local SEO success.