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.

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="<?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?

share|improve this question
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

This is how I would do it.

<?php
$today = getdate();

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

// Do your loop... thingy.
share|improve this answer
1  
Please explain why that could solve the problem. –  toscho Mar 2 '13 at 23:14
add comment

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

share|improve this answer
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.