admin管理员组

文章数量:1122846

I have a queried list of calendar events that I am trying to check if they have custom taxonomy terms. Within the loop, I have a list of "if" statements for outputting terms from the Accessibility Options taxonomy for each instance, like so:

if ( has_term('closed-captions', 'accessibility-options') ) { echo 'CC'; }

None of these work. However, when I do this:

$access_options = get_the_term_list( $post->ID, 'accessibility-options', '', ', ', ' ' );
echo $access_options

It outputs the expected terms. I have tested it by putting an exact post ID into the has_term statement and gotten the expected results. But it does work as is or when I use $post->ID in the parameters.

I tried outputting the post ID to check that, and when I do, I don't get the actual post ID. For example, if I am expecting the post ID to be 48445. I instead get 10000854.

Any insights are appreciated!

I have a queried list of calendar events that I am trying to check if they have custom taxonomy terms. Within the loop, I have a list of "if" statements for outputting terms from the Accessibility Options taxonomy for each instance, like so:

if ( has_term('closed-captions', 'accessibility-options') ) { echo 'CC'; }

None of these work. However, when I do this:

$access_options = get_the_term_list( $post->ID, 'accessibility-options', '', ', ', ' ' );
echo $access_options

It outputs the expected terms. I have tested it by putting an exact post ID into the has_term statement and gotten the expected results. But it does work as is or when I use $post->ID in the parameters.

I tried outputting the post ID to check that, and when I do, I don't get the actual post ID. For example, if I am expecting the post ID to be 48445. I instead get 10000854.

Any insights are appreciated!

Share Improve this question asked Oct 7, 2024 at 19:51 GrabBag-CharlesGrabBag-Charles 211 bronze badge 1
  • 1 are you setting the current post in a standard loop? If you try to be fancy and use get_posts without calling set_current_post( then WordPress won't be able to guess which post you're referring to when you call has_term but I can't tell from your code as the surrounding post loop is missing. There's a high chance the problem is in code not shared! – Tom J Nowell Commented Oct 7, 2024 at 20:27
Add a comment  | 

1 Answer 1

Reset to default 0

If your second code block is in the same code block as your first, then adding $post->ID as the third parameter of the has_term() call should fix it:

if ( has_term( 'closed-captions', 'accessibility-options', $post->ID ) ) { echo 'CC'; }

If it doesn't then as Tom mentioned, please update your question to include the surrounding relevant code.

本文标签: wp queryhasterm not returning anything