2

I've been scratching my head all day with this one. I'm trying to use has_term but it just won't work. I do get this notice but I'm not 100% sure it's related. I've also tried is_object_in_term() but that doesn't seem to do anything in this case.

Notice: Undefined property: stdClass::$object_id in /wp-includes/taxonomy.php on line 3613

if( has_term('past-attendees', 'speakercategory' ) ): 
    echo 'test'; 
endif;

if(is_object_in_term( $post->ID, 'speakercategory', 'past-attendees' ) ):
    echo 'test'; 
endif;

wp_get_object_terms() does return the term so I can't get my head around why the above two examples didn't work. Any ideas would be greatly appreciated.

5
  • Do you get this in a vanilla 4.4 install? Where are you testing this?
    – birgire
    Commented Dec 9, 2015 at 18:15
  • 1
    What happens if you pass the $post object into the has_term as the last parameter, do you get the correct result?
    – Howdy_McGee
    Commented Dec 9, 2015 at 22:51
  • @birgire Not really, it's a custom theme, has a handful of plugins installed. Commented Dec 10, 2015 at 9:37
  • @Howdy_McGee Same thing when I pass $post. The variable is defined right before that call. Commented Dec 10, 2015 at 9:38
  • Did you check if $post is not empty? To me looks like $post->ID is null, that's why you are getting the notice message when using is_object_in_term(). Can you share the result for var_export($post); ?
    – Pabamato
    Commented Aug 14, 2018 at 4:40

1 Answer 1

1

You mention in a previous comment that you are defining $post directly before the conditional statements. Since this is the case you need to pass the $post object to both these functions to get desired results otherwise WordPress will attempt to use the global $post object which isn't what you've defined and thus throwing errors or unexpected results.

Parameter three for has_term( $term, $taxonomy, $post ) says:

$post
(integer|object) (optional) Post to check instead of the current post.
Default: null

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.