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 ID
s with each Loop.
$do_not_duplicate[] = $post->ID;
I think the space you are seeing is the result of your echo
ing 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="https://wordpress.stackexchange.com/questions/139379/<?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="https://wordpress.stackexchange.com/questions/139379/<?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' )