It occured to me that other people might be interested in how to get WordPress to display any posts from the most recent date with posts, so here goes. It’s really not that complicated, but maybe it’ll help somebody out. We start with a basic WordPress Loop:
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<?php the_date('','<h2 class="align-right">','</h2>'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>We couldn't find any posts.</p>
<?php endif; ?>
We only want posts from the most recent date that any posts were made, so the first thing to do is get that date. To do this we add a line of code at the beginning of the loop before anything else:
<?php query_posts('showposts=5'); if(have_posts()): while(have_posts()): the_post(); ?>
<?php $currentDate = get_the_time('F j, Y'); ?>
<?php the_date('','<h2 class="align-right">','</h2>'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>We couldn't find any posts.</p>
<?php endif; ?>
Here, we’re using WordPress’s get_the_time function to get the date of the current post that the loop is processing in a format of our choosing and storing it in a variable. We can’t use the_date because it will return empty on subsequent calls if the date is the same for both posts. We also can’t use the_time This variable will change each time the loop grabs a post with a different date, so next we need to set a constant:
<?php $i = 0; ?>
<?php query_posts('showposts=5'); if(have_posts()): while(have_posts()): the_post(); ?>
<?php
$currentDate = get_the_time('F j, Y');
if($i == 0){define('LASTDATE', $currentDate};}
$i++;
?>
<?php the_date('','<h2 class="align-right">','</h2>'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>We couldn't find any posts.</p>
<?php endif; ?>
We use $i to check if this is our first time through the loop; if it is, we define a new constant, LASTDATE, with the date of the first post. Afterwards, we increment $i to make sure the constant is only set once.
Finally, we need to check $currentDate against LASTDATE. If the two are different, we break the loop:
<?php $i = 0; ?>
<?php query_posts('showposts=5'); if(have_posts()): while(have_posts()): the_post(); ?>
<?php
$currentDate = get_the_time('F j, Y');
if($i == 0){define('LASTDATE', $currentDate};}
if($currentDate != LASTDATE){break;}
$i++;
?>
<?php the_date('','<h2 class="align-right">','</h2>'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>We couldn't find any posts.</p>
<?php endif; ?>
And that’s it. Now only that last day’s worth of posts (whatever day that is) will show on the homepage. Note that if you’re homepage is a static WordPres Page you’ll need to use query_posts to get actual blog posts instead of the page content.
Update: If anyone knows how to stop the WordPress visual editor from stripping spaces out of <pre> tags that would be great. I installed the Save My <pre> plugin, but it isn’t working that well with spaces. Full indents seem to work, so I’ve updated the post accordingly.
