admin管理员组

文章数量:1288023

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.

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.

Share Improve this question edited Sep 7, 2021 at 16:34 robert0 asked Sep 7, 2021 at 16:23 robert0robert0 2032 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: phpIf post ID hasterm