How to show only today’s post?

I want to show only today’s or current date post in my blog.
Here is my code but it’s not working.

<?php   
$day = date('j');   
query_posts('day='.$day);   
if (have_posts()) :
while (have_posts()) : the_post();  
?>
<h3><a href="https://wordpress.stackexchange.com/questions/12152/<?php the_permalink() ?>" rel="bookmark" title="view: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="storycontent"><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>

How to solve this?

4 Answers
4

This should do the trick:

<?php
$day = date('Ymd');
query_posts("m=$day");

The query arg 'm' for WordPress will be interpreted as a year if it is four characters long, a month if 6, and a date if 8.

EDIT

You should probably add in the original query string just in case you’re writing enough to need things like pagination:

<?php
global $query_string;
$query_string = empty($query_string) ? 'm=' : $query_string . '&m=';
$query_string .= date('Ymd');
query_posts( $query_string );

That will allow you to preserve other query arguments that are supposed to be there.

Leave a Comment