Check if a post has any term in this custom taxonomy?

This,

if( has_term( 'jazz', 'genre' ) ) {
    // do something
}

will check if a post has the term jazz from the custom taxonomy genre. But how to check if a post belongs to a custom taxonomy genre? No matter whatever term it has, as long as it has something from the genre taxonomy, it will check.

So something like this,

if ( has_taxonomy('genre') ) {
    // whether it's jazz, blues, rock and roll; doesn't matter as long as the post has any of them.
}

2 s
2

You can have the term input empty, e.g.

if( has_term( '', 'genre' ) ) {
    // do something
}

to see if the current post object has any terms in the genre taxonomy.

It uses is_object_in_term() where:

The given terms are checked against the object’s terms’ term_ids,
names and slugs. Terms given as integers will only be checked against
the object’s terms’ term_ids. If no terms are given, determines if
object is associated with any terms in the given taxonomy.

Leave a Comment