How to Query WordPress Categories, Tags & Custom Taxonomies!

3.1428571428571 from 5
from 14 user
(Rate this post)
How to Query WordPress Categories, Tags & Custom Taxonomies!

Learn how to list Categories, Tags, and Custom Taxonomies in WordPress using standard WP functions. In this WordPress tutorial for developers, we use get_categories, get_tags, and get_taxonomies functions with working examples. After reading this tutorial, you can list WordPress categories, tags, and taxonomies with all available filters.

Key Points in WordPress Taxonomy Tutorial

Before we begin the coding part of the tutorial, I would like to talk a little about WordPress Taxonomies.

What are Taxonomies in WordPress?

Taxonomies are the mean of organizing WordPress posts. WordPress uses Taxonomies to label posts to organize them on different shelves.

In WordPress, categories & tags are taxonomies. Also, developers can add custom taxonomies to make advanced organizing options for their items (Posts, Pages, Custom post types, etc.).

What are Terms inside a Taxonomy?

Each item inside a taxonomy is a Term.

You can have multiple terms inside a taxonomy.

For example, Tag is a Taxonomy, and each item you know as a tag is a Term for “Tag taxonomy”. Same thing for Category and each “Category Item”.

What are the default Taxonomies in WordPress?

WordPress has default taxonomies that we use inside a WordPress panel regularly.

Categories, Tags and Post Formats are default taxonomies in WordPress.

Query Taxonomies in WordPress

get_terms is the primary function we use to query taxonomies, but they are prettier functions available for categories and tags that run get_terms under the hood.

Now if you want to get into action, remember these arguments because we will use them to filter the terms of each taxonomy.

Default taxonomy arguments

array(
   'taxonomy'=>//only in get_terms
   'object_ids'   =>
   'orderby' =>
   'order'   =>
   'hide_empty'   =>//default is true!
   'include' =>
   'exclude' => 
   'exclude_tree' => 
   'number'  => 
   'offset'  => 
   'fields'  => 
   'count'   => 
   'name'    => 
   'slug'    => 
   'term_taxonomy_id'  => 
   'hierarchical' => 
   'search'  => 
   'name__like'   => 
   'description__like' => 
   'pad_counts'   => 
   'get'=> 
   'child_of'=> 
   'parent'  => 
   'childless'    => 
   'cache_domain' => 
   'update_term_meta_cache' => 
   'meta_query'   => 
   'meta_key'=> 
   'meta_value'   => 
   'meta_type'    => 
   'meta_compare' => 
);

To those who want to see some code in this tutorial:

Developing WordPress is simple and fun, But if you want to run any code, you better know what it does and how it can affect your website.

I highly recommend you try codes on locally located WordPress installation before trying it online.

Also, you better create a WordPress plugin first, then use codes inside it to have better control over them.

Objects returned by the get_taxonomies function in WordPress

You will receive an array of WP_Term objects when you use get_taxonomies to retrieve taxonomy items.

Note: get_categories and get_tags use get_taxonomies under the hood.

WP_Term object has useful attributes, which are listed below:

WP_Term Object {
    [term_id]
    [name]
    [slug]
    [term_group]
    [term_taxonomy_id]
    [taxonomy]
    [description]
    [parent]
    [count]
    [filter]
    [meta]
}

Query Categories in WordPress

get_categories function in WordPress is available to retrieve category objects (WP_Term objects).

This function accepts arguments inside an array (just like query posts using WordPress).

get_categories function runs get_terms function under the hood. It’s just a little more readable.

Query All Categories in WordPress

get_categories with argument hide_empty set to false will give us all categories in WordPress:

//retrieving all posts using empty args
$args = array(
	"hide_empty" => false//to get all categories
);
$categories = get_categories($args);

//printing category name
foreach($categories as $category){
	echo $category->name." <br />";
}

Query Top Level Categories in WordPress

We use get_category function with parent argument set to 0:

//retrieving all posts using parent id set to 0
$args = array(
	"hide_empty" => false,
	"parent" => 0
);

//retrieve categories
$categories = get_categories($args);

//printing category name
foreach($categories as $category){
	echo $category->name." <br />";
}

Query Children of a Category in WordPress

parent argument is the key to doing our task. This argument is used to query children of a specific category (using category ID).

In this example, we will use the parent category id set to 1 (change it to your category id):

//retrieving posts using parent id
$parentCategoryId = 1;
$args = array(
	"hide_empty" => false,
	"parent" => $parentCategoryId
);

//retrieve categories
$categories = get_categories($args);

//printing category name
foreach($categories as $category){
	echo $category->name." <br />";
}

Order Categories in get_categories

You can manage category orders using orderby and order arguments in get_categories().

orderby is defining order basis (‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’, ‘parent’, ‘term_order’) and order is the direction (ASC or DESC).

This example shows you how to order categories based on the category name in ascending (ASC) order:

//order by name
$args = array(
	"hide_empty" => false,
	"orderby" => "name",
	"order" => "ASC",
);

//retrieve categories
$categories = get_categories($args);

//printing category name
foreach($categories as $category){
	echo $category->name." <br />";
}

Query Tags in WordPress

get_tags function is responsible for retrieving tags with desired arguments.

get_tags() runs get_terms function under the hood. Then it retrieves an array of WP_Term objects which are the tags.

Query Tags with a Word in the Tag Name

name__like is the argument to retrieve tags based on the name in WordPress using get_tags function:

//set the word
$word = "test";

//order by name
$args = array(
	"hide_empty" => false,
	"name__like" => $word
);

//retrieve tags
$tags = get_tags($args);

//printing tag name
foreach($tags as $tag){
	echo $tag->name." <br />";
}

Query Tags with a Word in the Tag Description

description__like is the argument that we can use with get_tags function to retrieve tags based on a word in their description:

//set the word
$word = "test";

//order by name
$args = array(
	"hide_empty" => false,
	"description__like" => $word
);

//retrieve tags
$tags = get_tags($args);

//printing tag name
foreach($tags as $tag){
	echo $tag->name." <br />";
}

Order Tags in get_tags

Like categories, we use orderby and order arguments to set the order basis and its direction for querying tags.

orderby is defining order basis (‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’, ‘parent’, ‘term_order’) and order is the direction (ASC or DESC).

//order by name
$args = array(
	"hide_empty" => false,
	"orderby" => "name",
	"order" => "ASC"
);

//retrieve tags
$tags = get_tags($args);


//printing tag name
foreach($tags as $tag){
	echo $tag->name." <br />";
}

Query Custom Taxonomies in WordPress

get_terms function in WordPress is available for querying custom taxonomies.

Like get_categories and get_tags functions, this function also receives arguments, but there is an extra taxonomy key in arguments to specify what type of taxonomy you want to retrieve.

//set taxonomy name
$taxonomyName = "taxonomy-name-here";

//set arguments for taxonomy
$args = array(
	"hide_empty" => false,
	"taxonomy" => $taxonomyName,
);

//retrieve terms
$terms = get_terms($args);

//printing term name
foreach($terms as $term){
	echo $term->name." <br />";
}

I will create a tutorial about adding Custom Taxonomies in WordPress; you better read it.

Continue learning about WordPress Taxonomies

A Professional WordPress Developer never stops learning. If you are interested in more information about taxonomies, I recommend visiting the WordPress official page of Taxonomies and Taxonomies for Developers.

Working with custom taxonomies on WordPress’s official website is also a great source of information for beginner developers.

StackExchange is a great place to ask questions or research other developers’ issues with taxonomies in WordPress.

What do you think about "How to Query WordPress Categories, Tags & Custom Taxonomies!"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu