In WordPress, determine whether a post has any given term out of a custom taxonomy

in code


This bugged the shit out of me yesterday, since WordPress doesn’t seem to have an equivalent to has_category() for custom taxonomies. If you don’t supply a term to check in the taxonomy, it checks all terms in the taxonomy.

function has_foo_taxonomy($term = null, $post_id = null) {
    $taxonomy = 'foobar';

    if ('' == $post_id) {
        $post_id = get_the_ID();
    }

    if ('' == $term) {
        $term_list = get_terms($taxonomy);

        foreach ($term_list as $a) {
            array_push($term, $a->term_id);
        }
    }

    return (has_term($term, $taxonomy, $post_id)) ? true : false;
}


Decompression

in me


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