I have a loop that displays the posts from Wordpress by category, but some posts are in two categories so on the home page where all the posts are shown, these post appears twice.
Example:
- "Example Post" is in "Category One" and "Category Two"
- When you see the posts of "Category One", "Example Post" is there. The same happens when you see the posts of "Category Two".
- But when you click "see all posts", "Example Post" shows twice (because it's in two categories)
Does anyone know how to "hide" this repeated posts?
This is the loop:
<?php
$loop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => -1 ) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$my_post_child_cats = array();
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(3, $childcat)) { ?>
<div class="item" data-groups='["all", "<?php echo $childcat->cat_name; ?>"]'>
<div class="pindex">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<div class="pimage">
<?php the_post_thumbnail(); ?>
<div class="pro-title">
<h4><?php echo get_the_title(); ?></h4>
<p>Read more</p></a>
</div>
</div>
<?php } ?>
</div>
</div>
<?php } } ?>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata();
?>
This code will get all the posts and add the category name with this piece: <?php echo $childcat->cat_name; ?>
Then in the home page I've created a filter by category which will take the posts by that $childcat->cat_name;
, but when you click "All posts" it will get the posts twice as it has two categories