Multiple loops in same page, without duplicate content

All we need is an easy explanation of the problem, so here it is.

I’m trying to make a page with multiple loops, I need every loop to display a predefined number of posts from the same category (every loop displays X number of posts from Category CATS). But I also need the second loop, the third loop and so on to display the posts from where the last loop stopped, without duplicating content (example: loop 1 from post 1 to 5. loop 2 from post 6 to 10. loop 3 from post 11 to 15 etc.). Here’s what I’ve tried so far (I also gave a try to the code in the wordpress codex, but nothing seems to work)

Can you help me?

here’s my code so far;

<?php   if (have_posts()) : ?>
<?php   $mosaics = new WP_Query('category_name=mosaics&posts_per_page=5'); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   if( $post->ID == $do_not_duplicate ) continue; ?>

<?php   echo get_the_content(); ?>
<?php   endwhile; ?>
<?php   endif; ?>
<?php   wp_reset_query(); ?>

<br />

<?php   if (have_posts()) : ?>
<?php   $mosaics = new WP_Query('category_name=mosaics&posts_per_page=4'); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   if( $post->ID == $do_not_duplicate ) continue; ?>

<?php   echo get_the_content(); ?>
<?php   endwhile; ?>
<?php   endif; ?>
<?php   wp_reset_query(); ?>

<br />

<?php   if (have_posts()) : ?>
<?php   $mosaics = new WP_Query('category_name=mosaics&posts_per_page=3'); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   if( $post->ID == $do_not_duplicate ) continue; ?>

<?php   echo get_the_content(); ?>
<?php   endwhile; ?>
<?php   endif; ?>
<?php   wp_reset_query(); ?>

<br />

<?php   if (have_posts()) : ?>
<?php   $mosaics = new WP_Query('category_name=mosaics&posts_per_page=2'); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   if( $post->ID == $do_not_duplicate ) continue; ?>

<?php   echo get_the_content(); ?>
<?php   endwhile; ?>
<?php   endif; ?>
<?php   wp_reset_query(); ?>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

it is important to read this Codex chapter all the way to the end, as the essential suggestions are after this heading ‘Note for Multiple Posts in the First Category’;

repetitive, tedious code:

<?php   $do_not_duplicate = array(); ?>
<?php   $mosaics = new WP_Query('category_name=mosaics&posts_per_page=5'); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   $do_not_duplicate[] = $post->ID; ?>

<?php   echo get_the_content(); ?>
<?php   endwhile; wp_reset_postdata(); ?>

<br />

<?php   $mosaics = new WP_Query( array( 'category_name' => 'mosaics', 'posts_per_page' => 4, 'post__not_in' => $do_not_duplicate ) ); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   $do_not_duplicate[] = $post->ID; ?>
<?php   echo get_the_content(); ?>
<?php   endwhile; wp_reset_postdata(); ?>

<br />

<?php   $mosaics = new WP_Query( array( 'category_name' => 'mosaics', 'posts_per_page' => 3, 'post__not_in' => $do_not_duplicate ) ); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   $do_not_duplicate[] = $post->ID; ?>
<?php   echo get_the_content(); ?>
<?php   endwhile; wp_reset_postdata(); ?>

<br />

<?php   $mosaics = new WP_Query( array( 'category_name' => 'mosaics', 'posts_per_page' => 2, 'post__not_in' => $do_not_duplicate ) ); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   $do_not_duplicate[] = $post->ID; ?>
<?php   echo get_the_content(); ?>
<?php   endwhile; wp_reset_postdata(); ?>

Method 2

While Michael’s answer works, doing multiple queries for this is a waste of database resources when you can get all of the results in a single query and handle breaking up output in PHP. See this answer I gave on another, similar question to achieve the same results with a single query to the database.

Method 3

Either use wp_reset_query() after every query in order to clear the results of the previous queries, or create WP_Query instances where loops would be iterated through local variables instead of the global ones.

Method 4

Use the Offset Parameter for the subsequent loops, this will start displaying posts after the number you specify:

// starts from post 6 to post 10
$mosaics = new WP_Query('category_name=mosaics&posts_per_page=5&offset=5');

// starts from post 11 to post 15
$mosaics = new WP_Query('category_name=mosaics&posts_per_page=5&offset=10');

Method 5

The magic.

 wp_reset_query();

That could be used to clean the way for further calls to WP_Query.
Or to avoid the duplication due a non structured call to the loop, such:

$data = query_posts($args);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply