1

I'm trying to find out whether the post has_term by post ID or not.

Right now I have this:

<?php $postid = $_GET['post_id'];
if( has_term( 'campaign', 'type' ) ): ?>
//Do something
<?php endif; ?>

How do I incorporate $postid variable inside the has_term?

So that I could check only that specific post id.

I have tried:

    <?php $postid = $_GET['post_id']; 
    if( has_term($postid, 'campaign', 'type' ) ): ?>

The above doesn't work.

Need help, thanks.

1 Answer 1

1

The 3rd parameter of has_term() accepts a WP_Post or Post ID. Docs on has_term(). In your example code, it would look like this:

<?php $postid = $_GET['post_id'];
if( has_term( 'campaign', 'type', $postid ) ): ?>
//Do something
<?php endif; ?>

Where campaign would be the term, type would be the taxonomy, and $postid is the post that may have the campaign term.

1
  • Brilliant! I thought I was missing something very small :) Thanks a lot for your help!
    – robert0
    Commented Sep 7, 2021 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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