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

$day = date('j');   
if (have_posts()) :
while (have_posts()) : the_post();  
<h3><a href="<?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?

  • 1
    The code in your question is missing the beginning of the while/if loops. Also when you say it is not working are you getting errors or not seeing results you are not expecting or ... ? – Manzabar Mar 15 '11 at 20:17
  • 1
    The code you've listed merely renders the result of the query you're looping through (as @Manzabar said). We need to see your entire loop so we know what you're querying the database for ... that will be where you're asking for today's post or for some other post. – EAMann Mar 15 '11 at 21:55

This should do the trick:

$day = date('Ymd');

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.


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

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.


I think you'll need to set the month and year also(possibly), see the Time Parameters for WP_Query, parameters shown throughout that page also apply to query_posts.


This is how I would do it.

$today = getdate();

query_posts( array(
    'year' => $today["year"],
    'monthnum' => $today["mon"],
    'day' => $today["mday"]
) ); 

// Do your loop... thingy.
  • 1
    Please explain why that could solve the problem. – fuxia Mar 2 '13 at 23:14

You could also do it without modifying the theme. In WP Admin: Settings > Reading > Blog pages show at most: 1 posts

  • 1
    That isn't a time-related change at all. Not only would that not do what Oronno asked, it would affect all the other 'archive' pages on the site, so there would only ever be one post showing on any page. – John P Bloch Mar 17 '11 at 13:40
  • Not to mention it assumes there will always be exactly one post per day. No more, no less. – John P Bloch Mar 17 '11 at 13:41
  • Maybe I misunderstood, I read "I want to show only today's or current date post in my blog" to mean he only wanted to show the most recent post on his blog. – gabrielk Mar 18 '11 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy