Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query posts (future & published) for current day. WordPress


I'm currently running a calendar based theme that features video game releases. I am trying to run a query that will display ONLY posts for the CURRENT day.

I've looked at creating a query with time parameters however I run into a problem. I have a bunch of scheduled posts that have been 'faked' to be 'published' rather than 'future' so that when the URL is accessed (sometimes months in advance) the post's page is accessible rather than spitting out a 404.

The problem now is that when I ran the query (I even found it from WPQuestions; See it grabs from the most "recent" post in the database which currently is for a video game that is set to release on 10/12.

I tested it out by setting various other games (on different days) to drafts and each time the post was set to a draft, the one before it (or rather the previous day) would be the "current" day.

Essentially what I need is to a.) allow all scheduled posts to be accessible via post URL (and not 404) and b.) to have a query on index.php that will display posts for the current (literal) day (eg: if today is 10/5 then only posts for 10/5 should show up.)

Answers (3)


Nilesh shiragave answers:

try this code...

$today = getdate();
query_posts('post_status=publish,future&year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>


enodekciw answers:

I would actually do it with custom fields.
Just set some field, for example 'release_date', and then query stuff using that custom field.

Something like this.

<?php $today = date('ymd'); ?>
<?php query_posts('meta_key=release_date&meta_value=' . $today . '&posts_per_page=-1'); ?>
<?php if(have_posts()) : ?>
<ul id="released-today">
<?php while(have_posts()) : the_post(); ?>
<li class="todays-release"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php endif; ?>

btw, for this to work, you should set 'release_date' custom field with something similar to this: 101005
10 - year, 10 - month, 05 - day.

For upcoming releases you can use this:

<?php $today = date('ymd'); ?>
<?php query_posts('meta_key=release_date&meta_compare=<&meta_value=' . $today . '&posts_per_page=-1'); ?>
<?php if(have_posts()) : ?>
<ul id="upcoming-releases">
<?php while(have_posts()) : the_post(); ?>
<li class="upcoming-release"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>
<?php endif; ?>

<strong>EDIT</strong>: modified upcoming releases query and loop.

Jordan Miskowicz comments:

Yeah, I definitely do NOT want to use a custom field for the date. That doesn't make sense seeing as though I'm already selecting the date/time itself.


Oleg Butuzov answers:

function custominit(){
if (!is_admin()){
add_filter('posts_where', 'posts_where_custom');

function posts_where_custom(){
global $wpdb;

$where .= " AND post_date <= '".date("Y-m-d 24:00:00")."'";
$where = str_replace($wpdb->posts.".post_status = 'publish'", $wpdb->posts.".post_status = 'publish' OR ".$wpdb->posts.".post_status = 'future'", $where);
return $where;