• Hi, I am trying to display a “featured posts” part on my page while on the rest of the page to display the other posts, and no duplicated posts should show up. I used the same code as in the wordpress documentation “The Loop”:

    <?php
    global $do_not_duplicate;
    $do_not_duplicate = array();
    ?>
    
    <?php
    $my_query = new WP_Query('cat=95&showposts=10');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate[] = $post->ID;
    ?>
    <!-- do stuff -->
    <?php endwhile; ?>
    
    <?php
    if (have_posts()) : while (have_posts()) : the_post();
    if (in_array($post->ID, $do_not_duplicate))
    {
    continue;
    }
    update_post_caches($posts);
    ?>
    <!-- do stuff -->
    <?php endwhile; endif; ?>

    It turned out only the first(latest) featured post was not duplicated, and the other featured posts all showed twice. I really cannot figure out what is wrong. Can someone help me out with this? Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Well, this works, so you could try this, changing categories etc. as appropriate:

    <?php $my_query = new WP_Query('cat=5&showposts=30&orderby=rand');
      if (have_posts()) :while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID;?>
    
    DO STUFF
    
    <?php endwhile; ?>
    <?php else : ?>
    <h2>You forgot to create any posts !</h2>
    <?php endif; ?>    
    
    <?php query_posts('order=DESC&showposts=100&cat=5&orderby=rand'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if(in_array($post->ID, $do_not_duplicate))continue;update_post_caches($posts);
    ?>
    DO STUFF
    
    <?php endwhile; ?> 
    
    <?php else : ?>
    
    <h2 class="not-found">Oops! You forgot to create any posts !</h2>
    
    <?php endif; ?>
    Thread Starter nahgnaw

    (@nahgnaw)

    @richarduk
    Thanks for the reply.
    But the major difference I see in our code is that you add a query_post()in front of your main loop. That actually is not necessary for me because I don’t need to adjust modify the way posts show in my main loop.
    I did try with that line of code, though. But it didn’t make any difference. Still every featured post except the latest one showed up in the main loop ??
    Another thing is when I tried to find out the problem, I printed the $do_not_duplicate array before each post. What was really weird was that the array was printed correctly with the latest featured post and each regular post before it, but the array just became empty after the latest featured post. I don’t know why this happened.

    Try adding <?php wp_reset_query();?> after each loop.

    And on the second loop, changing as appropriate, try something like this:

    $second_loop = new WP_Query(array('showposts' => 30,'cat' => 113,'post__not_in' => $loop1_output,'orderby' => rand,));
    if ($second_loop->have_posts()) : while ($second_loop->have_posts()) : $second_loop->the_post();

    This will make sure that the array (hopefully) only contains information from the first loop

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Multiple loops & featured posts’ is closed to new replies.