I have the following code in a template that used to work until I upgraded to Wordpress 4.9.7

if (in_category( 'vid' )) {
    $vidliclass = ' class="vid"';
} else {
    $vidliclass = '';

Since the upgrade, posts that are in category vid don't return true anymore.

I used the following code, to check on the category names of the posts:

$postcat = get_the_category( $post->ID );
if ( ! empty( $postcat ) ) {
    echo esc_html( $postcat[0]->name );   

And the posts that are in category "vid" are echoed as vid.

I also tried to replace "vid" with the category ID.

Can I replace in_category() with something else to check if the post is in category "vid"?

  • in which template do you use this template tag? are you using it inside of the loop? – David Sword Aug 2 '18 at 15:53
  • What context are you using it in? Are you inside the loop? In your second bit of code, where is $post coming from? Also, which version did you update from? 4.9.6 or something earlier? – Jacob Peattie Aug 2 '18 at 15:54
  • I updated from WordPress 4.9.1 – Philip Aug 2 '18 at 16:19
  • I am using the code while looping through a get_posts query. $index_query = get_posts( array( 'include' => $index_query_filled, 'orderby'=>'rand', ) ); foreach ( $index_query as $post ) : setup_postdata( $post ); – Philip Aug 2 '18 at 16:20

You're using in_category() outside of a loop, so you need to pass the post ID in as the 2nd argument.

eg, if (in_category( 'vid', $post->ID )) {

See Codex.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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