Displaying all taxonomies without custom posts?

This is one of those situations where a helper method might look like the right solution, but brings with it baggage that gets annoying when you try and do serious work with it.

Instead, ignore get_categories, and use the real API one level down that applies to all taxonomies, not just categories.

First, we need to correct a misconception. You are not displaying taxonomies. You are displaying terms in a taxonomy ( a custom taxonomy ). This might sound pedantic, but it’s essential to understanding the API. If you don’t make this distinction, things will get confusing, even though really they’re simple.

  • A taxonomy is a way of grouping/filtering/classifying things, e.g. tag, category, colour, company, type
  • A term is a particular instance of that taxonomy, e.g. red, blue, green, yellow are all terms in the colour taxonomy.

So you are trying to show all the terms in a taxonomy. This is important because if you search for that, you get results. If you search for how to display taxonomies, you get far fewer results.

This leads us to the more general function get_terms, the first example of which answers your question:

$terms = get_terms( array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
) );

Like the get_categories function, it returns WP_Term objects. Here’s another example from the official docs:

$terms = get_terms( array(
    'taxonomy' => 'my_taxonomy'
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li>' . $term->name . '</li>';
    }
    echo '</ul>';
}

Additional General Notes

  • Your code has an excessive number of <?php and ?> tags. It must takes a long time to type all those unnecessary opening and closing PHP tags. You don’t need to close PHP tags at the end of every line. If you have 5 lines of PHP you can get away with a single opening PHP tag, then the 5 lines, then the closing tag. If the last line of your files is PHP, you don’t need the closing tag either
  • Notice how the functions use arrays instead of strings for arguments. The old get_categories('taxonomy=instructors_countries&post_type=instructors') style is a tiny bit slower, and prevents you from using more advanced features. For example, it’s not possible to do a meta_query or tax_query in WP_Query if you pass it in as a string. Use array( 'taxonomy' => 'instructors_countries' .. etc ) instead. You can even put it across multiple lines for easy reading this way!
  • In your example, you fetch the categories then immediately go into a for loop. But what if no categories were found? Or something else went wrong? In those situations a WP_Error might have been returned. Now you have a PHP error log full of warnings or fatal errors 🙁
    `