Diwakar Academy

  • Home
  • WordPress
  • About Us
  • Contact Us
You are here: Home / WordPress / How to Check if Post has Taxonomy Term

September 23, 2023

How to Check if Post has Taxonomy Term

Spread the love

How to Check if Post has Taxonomy Term? Here’s something I learned while working with Custom Post Types and Custom Taxonomies. Typically, when you want to see if a regular WP Post belongs to a specific category, you can use the WordPress function called “in_category().” However, this method doesn’t work with Custom Post Types. Instead, when you want to check if a Custom Post Type is associated with a specific term in a Custom Taxonomy, you should use “has_term().”

Table of Contents

Toggle
  • Check if WP Post belongs to specific category
  • Check if CPT belongs to specific taxonomy term

Check if WP Post belongs to specific category

To find out if the current post belongs to a specific category, you can use “in_category().” For instance, in your theme’s “single.php” template, you can do it like this:

if (in_category(1)) {

// post is in category with ID = 1

}

In this example, we’re checking if the post belongs to the category with ID = 1. You can change that to any category ID, name, or slug, or even use an array to check for multiple categories, like this:

if (in_category('donuts')) {

   // post belongs to "donuts" category

} elseif (in_category(array('coffee', 'beer'))) {

  // post belongs to either "coffee" or "beer"

} else {

  // post does not belong to any of the above categories

}

Take a look at how we’re using an array in the “elseif” condition. You can list as many categories as you want by using an array with their IDs, names, or slugs.

Check if CPT belongs to specific taxonomy term

Now, let’s get to the main point of this tutorial. To see if the current post is associated with a specific term in a custom taxonomy, like “download_category,” and you want to check if it belongs to the term “combo,” you can do this:

if (has_term('combo', 'download_category')) {

     // post belongs to "combo" in "download_category" taxonomy

}

When you use “has_term(),” you provide the term’s name as the first parameter and the taxonomy’s name as the second parameter.

If you want to check for multiple terms, you can use an array that includes term IDs, names, or slugs. For instance:

if (has_term(array('combo', 'book', 'deal'), 'download_category')) {

    // post belongs to "combo", "book", or "deal" in "download_category" taxonomy

}

In this example, it will check if the current post is associated with either “combo,” “book,” or “deal” in the “download_category” taxonomy.

If you want to check if the current post is associated with any term in a specific taxonomy, just leave the first parameter empty or blank, like this:

if (has_term('', 'download_category')) {

   // post belongs to a term in the "download_category" taxonomy

}

In this example, we’re checking if the current post is linked to any term in the “download_category” taxonomy.

So, here’s the key takeaway:

– To see if a post is in a category, use “in_category()”.

– To see if a post has a specific tax term, use “has_term()”.

Related Articles

  • Stay Logged in to WordPress
  • How to View Word Count in WordPress for Any Post or Page

Article by Diwakar Academy / WordPress Leave a Comment

Leave a Reply Cancel reply

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

  • Home
  • WordPress
  • About Us
  • Contact Us

Copyright © 2025 · All Rights Reserved · Terms and Conditions · Privacy Policy