7

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.
}
1
10

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.

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

would return true if the post contains any term in the genre taxonomy

2
  • I don't think the function has_taxonomy() exists in the WordPress core. – birgire Dec 18 '16 at 14:47
  • 1
    Sorry, that was a mistake. Changed has_taxonomy to has_term – Tunji Dec 18 '16 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.