Alex Turnwall designer + developer
Learn, WordPress

WordPress: find posts with ANY term in a given taxonomy


I was recently Googling around for an answer to this, and didn’t see that anyone posted this method, but here’s the gist.

If you want to find out if a given post has a certain taxonomy, (not just a specific term, but ANY term in that taxonomy), you can use the function has_term (more in the codex):

<?php has_term('', $taxonomy_name); ?>

The first blank set of quotes essentially means it’ll return true if any term exists within the given taxonomy.

For example, let’s say you’re writing about books and have a custom taxonomy called “genre.” Your genres, or terms might be something like “fiction,” “mystery,” “sports,” “history,” etc. In this example, if you wanted to find out if a post was given the genre “mystery,” you could use the following code:

<?php has_term('mystery', 'genre'); ?>

And if you simply wanted to see if a post is assigned any genre, you’d use the empty first term version:

<?php has_term('', 'genre'); ?>

Think about this in the simpler conditionals for WordPress categories: <?php in_category( $category ); ?> and <?php is_category( $category ); ?> works.

But the same is not true for custom taxonomies. The function is_tax( $taxonomy ) works as expected, but there is no corresponding in_tax( $taxonomy ) function.

Instead, use the code above!

Leave a Reply

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