Categories Cat_ID in WordPress PHP

Sheeraz Gul Sep 22, 2022
Categories Cat_ID in WordPress PHP

This tutorial demonstrates how to get cat_id in PHP.

Categories cat_id in WordPress PHP

The categories are the essential part of a website, and WordPress provides a lot of functionalities regarding the categories. Generally, the categories are saved with two mandatory options, one the id and the other their name.

When we are working with categories and need to use them on our website, we need an id from which we can call particular categories or get the information about the particular category using that ID.

Get All Cat_IDs WordPress

WordPress provides the following method to get all the information about categories.

get_the_category( $id )

The above method will return all the categories of the current post or page or the categories for the given post id. The method will return an array, and that array will include the following objects.

term_id
cat_ID
object_id
term_taxonomy_id
name
slug
term_group
taxonomy
description
parent
count
filter
category_count
category_description
cat_name
category_nicename
category_parent

As we can see, the array also returns the term_id and cat_ID, which can be used to call a particular id. Here are some simple examples.

  1. Get ids for all categories in the current post:

    $All_Categories = get_the_category();
    	foreach( $All_Categories as $Each_Category ) {
    		echo  $Each_Category->term_id . '<br />';
    }
    

    The code above will print the term_id for each current post or page category. And if you want to do it for a specific post, you need to put an id in the method.

    $All_Categories = get_the_category($Post_ID);
    	foreach( $All_Categories as $Each_Category ) {
    		echo  $Each_Category->term_id . '<br />';
    }
    
  2. Get Names for all categories in the current post:

    $All_Categories = get_the_category();
    	foreach( $All_Categories as $Each_Category ) {
    		echo  $Each_Category->name . '<br />';
    }
    

    Similarly, this code will return the name for the current post or page.

  3. Get cat_ID for all categories in the current post:

    $All_Categories = get_the_category();
    	foreach( $All_Categories as $Each_Category ) {
    		echo  $Each_Category->cat_ID . '<br />';
    }
    

Get Cat_ID by Category Name in WordPress

But what if we want to get the cat_ID for a particular category by their name? WordPress also provides a function to get the cat_ID by the category’s name.

The method is:

get_cat_ID( string $category_name )

By giving the category name in the above method, we can get the cat_ID in an integer using the method get_cat_ID(). See the example.

$Category_id = get_cat_ID( $category_name );
echo Category_id;

The code above will return the cat_ID for the given category name.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook