Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?>
share|improve this question
    
'gave a try to the code in the wordpress codex' - are you referring to: codex.wordpress.org/The_Loop#Multiple_Loops_in_Action –  Michael Nov 11 '12 at 14:32
    
@Michael: yes, I tried that and other stuff but nothing seems to work –  Martin Aston Nov 11 '12 at 14:45
add comment

4 Answers

up vote 2 down vote accepted

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(); ?>
share|improve this answer
    
thank you very very very much Michael. that works perfectly! :) –  Martin Aston Nov 11 '12 at 16:43
add comment

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.

share|improve this answer
    
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 '12 at 18:25
add comment

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.

share|improve this answer
1  
Nope, that alone would not prevent duplicate posts, because one post can have multiple categories. –  toscho Nov 11 '12 at 14:25
    
right to the spot. Thanks for noticing. –  Mario Peshev Nov 11 '12 at 20:29
add comment

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');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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