I have a custom post type called testimonials that are assigned to two categories. At the moment I have three testimonials in total and despite doing a check to make sure the testimonial has a category set and it is equal to the category I need, the post with out any category assigned still shows up. My code is as follows
<?php
$my_query = new WP_Query( array('post_type' => 'testimonial', 'showposts' => '1', 'orderby' => 'rand'));
while ( $my_query->have_posts() ) : $my_query->the_post();
$categories = get_the_category();
$cat = $categories[1]->term_id;
var_dump($cat);
?>
<?php if(isset($cat) && in_category($cat)): ?>
<?php the_content(); ?>
<p>- <?php the_field('testimonial_author'); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>