Nice to see ClassicPress still going strong! :)
Digging Into WordPress v5.5
Book updates! New versions of all books including Digging Into WordPress now available for download. As always, updates are FREE for all book owners :)
New Bookstore!
Excited to announce the launch of our new bookstore! It's got a minimalist design and loads super fast. Go there to get great books like Digging Into WordPress, The Tao of WordPress, and WordPress Themes In Depth. Check it out and learn more about how the site was built.
How to Check if Post has Taxonomy Term
Something I did not know about when working with Custom Post Types and Custom Taxonomies. Normally when checking if a regular WP Post belongs to a specific category, we can use the WordPress function in_category(). But that does not work with Custom Post Types. To check if a CPT belongs to a specific term in a Custom Taxonomy, use has_term() instead.
Check if WP Post belongs to specific category
To check if the current post belongs to a specific category, use in_category()
. For example in your theme's single.php
template, you can do this:
if (in_category(1)) {
// post is in category with ID = 1
}
Here we are checking if the post belongs to category with ID = 1. You can change that to any category ID, name or slug, or an array containing multiple values.
Here is an example where mutliple categories are checked:
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
}
Notice the use of an array in the elseif
condition. You can specify as many categories as needed using an array of category IDs, names, or slugs.
Check if CPT belongs to specific taxonomy term
Now for the main point of this tutorial. To check if the current post belongs to a specific term in a custom taxonomy. For example, if we have a taxonomy named download_category
and want to check if the current post belongs to the term combo
, we can do this:
if (has_term('combo', 'download_category')) {
// post belongs to "combo" in "download_category" taxonomy
}
When calling has_term()
, the first parameter is the name of the term, and the second parameter is the name of the taxonomy.
To check multiple terms, use an array of term IDs, names, or slugs. For example:
if (has_term(array('combo', 'book', 'deal'), 'download_category')) {
// post belongs to "combo", "book", or "deal" in "download_category" taxonomy
}
So this example will check if the current post belongs to "combo", "book", or "deal" in the "download_category" taxonomy.
Bonus Tip: Check for *any* taxonomy term
To check if the current post belongs to any term in a given taxonomy, simply leave the first parameter empty/blank. Example:
if (has_term('', 'download_category')) {
// post belongs to a term in the "download_category" taxonomy
}
Here we are checking if the current post belongs to any term in the "download_category" taxonomy.
That's the thick and thin of it.
Bottom line is just remember:
- Check post for category — use
in_category()
- Check post for tax term — use
has_term()
Pro Plugin Giveaway
Yay this year is the 7th birthday of our pro WordPress plugin site, Plugin-Planet.com. To celebrate the event, we are giving away 7 free copies of our premium WordPress plugins!
View Custom Fields Meta Box in Gutenberg Block Editor
I get bunches of emails asking what happened to the "Custom Fields" meta box on the "Edit Posts" screen. They're hidden by default with the new Block Editor, so questions like, "do I need to install a plugin to get them back again?" No you don't. To view the Custom Fields for any post click the three dots in the upper-right corner of the screen, and then go to Options. There you will find a checkbox to enable Custom Fields (under "Advanced Panels"). Scroll down the page and you will see the Custom Fields meta box.