Display A List of Custom Taxonomy Terms with Posts Number

A WordPress taxonomy is a way to organize groups of posts and custom post types. By default, WordPress comes with two taxonomies called categories and tags. However, if you are using a custom post type, then you may need custom taxonomies.

There are two types of custom taxonomies.

  1. Hierarchical Taxonomy (works like categories)
  2. Non-Hierarchical Taxonomy (works like tags)

For instance, you can create a custom post type called ‘Quotes’ and sort it using custom taxonomies ‘Topics’ and ‘Writers’. Each topic and each writer is a term.

What if you want to display a list of topics or a list of writers with their posts number?

WordPress has pre-made categories and tag cloud widget. But there is no widget to show custom taxonomy terms list.


Categories widget’s output will be something like this:

Category Widget

How can we achieve similar result for custom taxonomies?

Add following code to the template where you want to show the list:

$custom_cats = get_terms('quote-writer');
echo '<ul>';
foreach ( $custom_cats as $term ) {
    $term_link = get_term_link( $term);
    echo '<li><a href="'.$term_link.'">'.$term->name . ' (' . $term->count . ')</a></li>';
echo '</ul>';

Change ‘quote-writer‘ to your custom taxonomy name. This works for both hierarchical and non-hierarchical taxonomy.

You can even use ‘category‘ or ‘post_tag‘ here to get the list of categories or tags.

Let me know if this works for you or not.

If you want to paginate the list of custom taxonomy terms, follow this tutorial: How to Paginate Taxonomy Terms with Posts Number

Spread the love

A teacher by profession, a traveler by passion and a netizen by choice.

Morshed Alam

You use WordPress! Why don't we share our experience! It may be a tutorial, tips, tricks or about security, performance or WordPress news. Write Today

2 thoughts on “Display A List of Custom Taxonomy Terms with Posts Number”

  1. How would you add a custom field already created?
    For example, you create a custom field with afc called “pdf” and you want the link to appear below. How do you make it work?
    Because I have been tested with:
    $ pdf = get_field (‘pdf’, $ term-> name. ‘_’. $ term-> term_id);
    And with:
    $ pdf = get_field (‘pdf’, $ term);

    but I can’t make it work.

Leave a Comment

Your email address will not be published. Required fields are marked *