1

We know that we can check if the particular post has a term by using this code:

has_term('term', 'taxonomy', $post->ID )) {

I was wondering if there is a code to check if a particular post does not have a particular term. Thanks.

  • I don't understand your question because you can use the same code for checking if a post doesn't have a term..? – Emetrop Aug 7 '15 at 11:40
7
if ( !has_term('term', 'taxonomy', $post->ID )) {

Use the NOT (!) operator

  • 2
    Added a bit more info as your post will auto flag as low quality ;-) – Pieter Goosen Aug 7 '15 at 11:46
1

This worked for me:

function myFunction() {
    $value = "";
    if( has_term( 'myterm', 'taxonomy' ) ) {
            $value = "foo";
    }
    elseif( has_term( 'nextterm', 'taxonomy' ) ) {
        $value = "nextfoo";
    }
        if( !empty( $value ) ) {
        echo 
        //do something with 
        $value;
    }
}

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.