0

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(); ?>
2

5 Answers 5

5

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(); ?>
0
3

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.

1
  • I totally agree; however, my answer is hopefully useful as a general layout on how to avoid duplicate posts with a multitude of loops.
    – Michael
    Nov 11, 2012 at 18:25
0

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.

1
  • 1
    Nope, that alone would not prevent duplicate posts, because one post can have multiple categories.
    – fuxia
    Nov 11, 2012 at 14:25
0

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');
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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