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

WP Query only future posts WordPress

  • SOLVED

I am building a simple calendar for a client. I used the [[LINK href="http://www.tomsdimension.de/wp-plugins/no-future-posts"]]No Future Posts Plugin[[/LINK]] to enable me to publish posts whose date are set for a future date.

My problem is now, that in this list of posts, I'd like to be able to show only posts <strong>today</strong> or in the <strong>future</strong>, no past posts so it's only upcoming events. Is this possible? How might I modify the wp_Query below to accomplish this?

<?php $my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5 ) );while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink() ?>"><strong><?php the_date( 'n/d'); ?></strong><?php the_title(); ?></a></li>
<?php endwhile; ?>

Answers (5)

2010-07-20

wjm answers:

hi,
your query should look like

new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'post_status' => 'publish,future', ) );

and based on
http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters
you can apply a filter (i modified the code to suit your needs)

<?php
//based on Austin Matzko's code from wp-hackers email list
function filter_where($where = '') {
$where .= " AND post_date >= DATE( NOW() )";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>


so you will end up with something like this,



<?php
//based on Austin Matzko's code from wp-hackers email list
function filter_where($where = '') {
$where .= " AND post_date >= DATE( NOW() )";
return $where;
}
add_filter('posts_where', 'filter_where');
$my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'post_status' => array( 'publish', 'future' ), ) );while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>

<li><a href="<?php the_permalink() ?>"><?php the_date( 'n/d'); ?><?php the_title(); ?></a></li>

<?php endwhile; ?>

<?php
remove_filter('posts_where', 'filter_where');
?>


I will try this code in my site and submit any improvement as i havent run it yet.


wjm comments:

ok. this is the revised version as the above had a little error,


<ul>
<?php
function filter_where($where = '') {
$where .= " AND post_date >= DATE( NOW() )";
return $where;
}
add_filter('posts_where', 'filter_where');
$my_query = new WP_Query(
array(
'post_type'=>'post',
'posts_per_page' => 6,
//'orderby' => 'date',
'order' => 'ASC',
'cat' => 5,
'post_status' => 'publish,future',
)
);
while ( $my_query->have_posts() ) :
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<li><a href="<?php the_permalink() ?>"><?php the_date( 'n/d'); ?> <?php the_title(); ?></a></li>
<?php
endwhile;
remove_filter('posts_where', 'filter_where');
?>
</ul>



and it works.
Note: the above code lists today's posts (they could have been published a couple of hours ago today and they will be still showed).
if you just want future, change
$where .= " AND post_date >= DATE( NOW() )";
to
$where .= " AND post_date >= NOW()";

you may want to uncomment
//'orderby' => 'date',
but i think posts are sorted by date by default.

let me know if you have any questions
- wjm

2010-07-20

Utkarsh Kukreti answers:

Use

Today's posts

$today = getdate();
$my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'year' => $today["year"], 'monthnum' => $today["mon"], 'day' => $today["mday"] ) );


Future posts

$my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'post_status' => 'future') );

2010-07-20

Oleg Butuzov answers:

just filter query... add post_date > NOW() for that


Oleg Butuzov comments:

as sample of query parse (btw its a bit related to your issue)

http://wpquestions.com/question/show/id/485


Oleg Butuzov comments:

oh... take a look to the solution posted <strong>wjm</strong>. same as i tald. and its perfectly fit your needs.

2010-07-20

Pippin Williamson answers:

Take a look at these links:

http://lorelle.wordpress.com/2006/10/01/working-ahead-future-posts-with-wordpress/

http://www.studionashvegas.com/tutorial/displaying-future-posts-in-wordpress/

http://wordpress.org/extend/plugins/display-future-posts/

2010-07-21

Darrin Boutote answers:


<?php
$my_query = new WP_Query( array(
'post_status' => 'future,publish',
'post_type'=>'post',
'posts_per_page' => -1,
'order' => 'ASC',
'cat' => 5 )
);

if ( $my_query->have_posts() ) :

while ($my_query->have_posts()) : $my_query->the_post();
if( strtotime($post->post_date) < time() ) {
//do nothing
} else { ?>
<li><a href="<?php the_permalink() ?>"> <?php the_date( 'n/d'); ?> <?php the_title(); ?></a></li>
<?php } ?>

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