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.

My home page has a section at the top that displays a featured post from a time period if it has been set, otherwise it displays the most commented post of a time period, or it just displays the most recent post if none of the above are true.

I then have two more loops on the page which display the 6 most recents posts (3 each).

I am using the following code to avoid duplicate posts being displayed.

<?php 

    $do_not_duplicate = $post->ID; 

    if( $post->ID == $do_not_duplicate ) continue;

?>

This does the job of avoiding duplicate posts by skipping over the post that matches the ID, however it leaves a blank space that makes the homepage look odd. Is there a way to display the next most recent post that isn't that ID, rather than skipping over it entirely?

<div class="grid_8 main-content">

                    <div class="grid_12 alpha omega">

                    <?php 

                    // Fetch from the 'featured' category

                    $args = array(                
                        'posts_per_page' => 1,                    
                        'category_name'  => 'featured',
                        'date_query'     => array( 'after' => '1 day ago' )
                    );

                    $the_query = new WP_Query( $args );

                    // If no featured post is found fetch the most commented

                    if ( 0 === $the_query->found_posts ) {  

                    $args = array(                
                            'posts_per_page' => 1,                    
                            'orderby'        => 'comment_count',
                            'date_query'     => array( 'after' => '1 day ago' )
                        );

                    $the_query = new WP_Query( $args );

                    }

                    // If no most commented posts are available fetch the most recent

                    if ( 0 === $the_query->found_posts ) {

                    $args = array(                
                            'posts_per_page' => 1,                    
                            'orderby'        => 'most_recent'
                        );

                    $the_query = new WP_Query( $args );

                    }


                    ?>

                    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                    <?php $do_not_duplicate = $post->ID; ?>

                        <a href="<?php the_permalink();?>"><h1 class="main-heading"><?php the_title(); ?></h1></a>
                        <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>

                        <?php the_excerpt(); ?>

                        <hr>

                    <?php 

                    endwhile; 

                    wp_reset_postdata();

                    else: 

                    _e('No posts found');

                    endif; ?>

                    </div>

                    <?php 

                    $args = array(
                    'posts_per_page' => 3,
                    'orderby' => 'most_recent'
                    );

                    $the_query = new WP_Query( $args );

                    ?>

                    <div class="recent_posts grid_6 alpha">

                    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                    <?php if( $post->ID == $do_not_duplicate ) continue; ?>

                        <a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
                        <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>
                        <?php the_excerpt(); ?>

                        <hr>

                    <?php endwhile; else: ?>

                    <?php endif; ?>



                    <?php wp_reset_postdata(); ?>



                    </div>

                    <?php 

                    $exclude = $most_recent;


                    $args = array(
                    'posts_per_page' => 3,
                    'orderby' => 'most_recent',
                    'offset'  => 3
                    );

                    $the_query = new WP_Query( $args );

                    ?>

                    <div class="grid_6 alpha">

                    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                    <?php if( $post->ID == $do_not_duplicate ) continue; ?>

                        <a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
                        <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>
                        <?php the_excerpt(); ?>

                        <hr>

                    <?php endwhile; else: ?>

                    <?php endif; ?>

                    <?php wp_reset_postdata(); ?>

                    </div>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I don't think your code does what you think, as it is only ever set to a single post from one of your loops. You description doesn't really match that logic.

To prevent duplicates you need to accumulate an array of IDs with each Loop.

$do_not_duplicate[] = $post->ID; 

I think the space you are seeing is the result of your echoing markup even though you loop may be empty. To avoid that, exclude your $do_not_duplicate values in the query itself and only print markup if there are posts to print.

The first is done with the post__not_in argument. The latter is done by putting your "wrapper" div inside the have_posts() check. And it helps to have readable code formatting. For example:

if ( $the_query->have_posts() ) { ?>
  <div class="grid_6 alpha"><?php
    while ( $the_query->have_posts() ) : 
      $the_query->the_post(); 
      $do_not_duplicate[] = $post->ID; ?>
      <a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
      <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>
      <?php the_excerpt(); ?>
      <hr><?php
    } ?>
  </div><?php
} 

Note: You were using a bare have_posts() in the if condition, which assumes $wp_query not the query you created. That could contribute to your problem.

I also notice that you have a date query. That should be an array of arrays. That is, this:

'date_query'     => array(array( 'after' => '1 day ago' ))

Instead of this:

'date_query'     => array( 'after' => '1 day ago' )
share|improve this answer

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.