has_term not working

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.

1 Answer
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

Leave a Comment