WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple Loops duplicate post question (3 posts)

  1. JoostJoost
    Member
    Posted 1 year ago #

    Hi

    In total I want to display 4 posts in which I created 2 loops where the goal of the first loop is to show the 2 latest posts.
    The second loop should display 2 posts random, excluding the first 2 of above. This works fine except for one thing:

    Instead of displaying 2 posts the second loop leaves the space empty if it selects one post from the first loop. Who can help me solve this issue?

    This is my code:

    <div id="vids">
    		<?php $my_query = new WP_Query('cat=-3,-6&posts_per_page=2&orderby=date&order=DESC');?>
    		<?php while ($my_query->have_posts()) : $my_query->the_post();
    
     $do_not_duplicate[] = $post->ID ?>
    
    	<div class="homevideo">
    	<div class="category">
    	<?php the_category(' '); ?>
    	</div>
    	<div class="videothumb">
    	<img src="<?php video_thumbnail(); ?>" width="185" height="105" />
    	</div>
    	<div class="videoname">
          <a href="<?php the_permalink() ?>" > <?php the_title(); ?>  </a>
        </div>
            </div>
    		<?php endwhile; ?>
    		<?php $my_newquery = new WP_Query('cat=-3,-6&posts_per_page=2&orderby=rand');?>
    <?php while ($my_newquery->have_posts()) : $my_newquery->the_post();
    
    if (in_array($post->ID, $do_not_duplicate)) continue;
     ?>
    	<div class="homevideo">
    	<div class="category">
    	<?php the_category(' '); ?>
    	</div>
    	<div class="videothumb">
    	<img src="<?php video_thumbnail(); ?>" width="185" height="105" />
    	</div>
    	<div class="videoname">
          <a href="<?php the_permalink() ?>" > <?php the_title(); ?>  </a>
        </div>
            </div>
    
    		 <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    thanks a lot in advance!

    Joost

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Take out the if test for in_array, and put the excluded post ids in the query:

    <?php
    $args = array(
       'cat' => '-3,-6',
       'posts_per_page' => 2,
       'orderby' => 'rand',
       'post__not_in' => $do_not_duplicate
    );
    $my_newquery = new WP_Query($args);?>
  3. JoostJoost
    Member
    Posted 1 year ago #

    Thank you so much. It works perfectly!

    Topic resolved!

Topic Closed

This topic has been closed to new replies.

About this Topic