How to Query WordPress Categories, Tags and Taxonomies

Learn how to list Categories, Tags and Taxonomies in WordPress using standard 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.

5 from 5
from 1 user
(Rate this post)

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 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 for categories and tags available, which runs the get_terms under the hood.

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

default taxonomy arguments are:

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 here:

Developing WordPress is simple and fun, But if you want to run any code, you better know what does it do and how can it 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 that plugin, so you will have better control over them.

Objects returned by get_categories function in WordPress

When you use get_categories to retrieve category items, you will receive an array of WP_Term objects.

Each term 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. This function accepts arguments inside an array parameter (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 Childrens of a Category in WordPress

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

In this example we will use 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 base on the category name in 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 is the function responsible for retrieving tags with desired arguments.

Query Tags with a Word in the Name

name__like is the helpful argument to retrieve tags 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 Description

description__like is the helpful argument that we can use with get_tags function:

//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

We use orderby and order arguments to set the order basis and its direction.

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. This function receives arguments like get_categories and get_tags, but there is a taxonomy argument to specify what type of taxonomy you want to retrieve.

I will create a tutorial about adding Custom Taxonomies in WordPress, You better read it.

//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 />";
}

Continue learning about WordPress Taxonomies

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

Working with custom taxonomies on WordPress 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 and Taxonomies"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu