Get category details by term ID

In a day to day development process, we need to display category details inside a post card or something else.

In order to get single category details by their term ID you need to pass ID in the get_term_by() WordPress function. Below is the example.

PHP
get_term_by('id', 'your_term_id', 'category');
PHP
get_term_by('id', 6, 'category');

You need to place single category ID inside that function. It returns a object like below.

PHP
object(WP_Term)#985 (10) {
  ["term_id"]=>
  int(6)
  ["name"]=>
  string(8) "Cat Four"
  ["slug"]=>
  string(8) "cat-four"
  ["term_group"]=>
  int(0)
  ["term_taxonomy_id"]=>
  int(6)
  ["taxonomy"]=>
  string(8) "category"
  ["description"]=>
  string(0) ""
  ["parent"]=>
  int(0)
  ["count"]=>
  int(4)
  ["filter"]=>
  string(3) "raw"
}

Multiple Category

If you have multiple category id in a array and want to get all the data once, then below snippet comes into play.

PHP
get_terms('category', [
    'include' => [id1, id2, id3, id4]
])

It will return array of object of all category details like below.

PHP
array(3) {
  [0]=>
  object(WP_Term)#994 (10) {
    ["term_id"]=>
    int(6)
    ["name"]=>
    string(8) "Cat Four"
    ["slug"]=>
    string(8) "cat-four"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(6)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(4)
    ["filter"]=>
    string(3) "raw"
  }
  [1]=>
  object(WP_Term)#995 (10) {
    ["term_id"]=>
    int(3)
    ["name"]=>
    string(7) "Cat one"
    ["slug"]=>
    string(7) "cat-one"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(3)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(3)
    ["filter"]=>
    string(3) "raw"
  }
  [2]=>
  object(WP_Term)#996 (10) {
    ["term_id"]=>
    int(1)
    ["name"]=>
    string(13) "Uncategorized"
    ["slug"]=>
    string(13) "uncategorized"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(1)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(6)
    ["filter"]=>
    string(3) "raw"
  }
}

Fetch from Rest API

While you are in custom block development process and need to fetch multiple category data from rest api, then below snippet comes into play.

PHP
apiFetch({
    path: `/wp/v2/categories?include=catid1,catid2`
}).then(res => {
    console.log(res);
}).catch(err => console.log(err))

You can not use apiFetch directly into your component. you need to import it first from @wordpress/api-fetch

PHP
import apiFetch from '@wordpress/api-fetch';

Note: Don’t use apiFetch directly inside you component. It might be end with unfinished loop. Because if you update any attribute while the promise resolve, it will run again. so it might be cause some bad effect in you site.

Try to use it with useEffect() with a dependency like below. Also you can not use this function directly unless you import it.

PHP
import { useEffect } from '@wordpress/element';
useEffect(() => {
    apiFetch({
        path: `/wp/v2/categories?include=${postArr}`
    }).then(res => {
        setCatData(res);
    }).catch(err => console.log(err))
}, [postArr])

Here is postArr is my dependency. Add it from you own component. I appreciate before you use it, check the doc from here.

Have a nice day !