In my loop, I have ten posts with the following categories (slug): group-a, group-a-first and group-b. The loop/query looks like this:
- Post (group-a)
- Post (group-b)
- Post (group-a-first)
What's the best way to check if those posts has a specific category?
I've tried <?php if(in_category('group-a')) ;?>
, it does work but it checks every post so I three results: yes, no, no, etc.
What I'm looking for if any post in the loop has a specific category, it outputs either a yes or no.
My query has multiple loops so this the code I'm using:
<?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published')))); $query = new WP_Query( $args );?>
<?php if ( $query->have_posts() ) : $duplicates = []; while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_category( 'first-major' ) ) : ?>
<div class="major"><div class="major-first">
major and first - <?php the_title();?><br>
<?php $duplicates[] = get_the_ID(); ?>
</div>
<?php endif; endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if (( in_category( 'major' ) ) && (!in_category( 'first-major'))) : if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
major - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?></div>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if (( in_category( 'major' ) ) && (!in_category( 'first-major'))) : if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
major - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?></div>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_category('group-a')) :?>
yes
<?php else :?>
no
<?php endif;?>
<?php if ( in_category( 'group-a-first' ) ) : ?>
group a and first - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if (( in_category( 'group-a' ) ) && (!in_category( 'group-a-first'))) : if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
group a - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_category( 'group-b-first' ) ) : ?>
group b and first - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
<?php the_title();?><br>
<?php endwhile; wp_reset_postdata(); endif; ?>
</section>
Thanks,