Question

How to display a list of posts from today date to future? I am actually using this code:

<div id="news-loop">
<?php if (have_posts()) : ?>
                <?php query_posts('cat=4&showposts=6&orderby=date&order=DESC&post_status=future&post_status=published'); while (have_posts()) : the_post(); ?>
                <p><?php the_time('j F, Y') ?></p>
                <p><a  href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
            </div>
            <?php endwhile; ?>
        <?php else : ?>
   <?php endif; ?>

This is showing correctly all the posts and future posts for that category.

An additional problem is: since I'm using "post_status=future&post_status=published" I have to trash the old posts to avoid them being displayed.

Thanks for your help!

Was it helpful?

Solution

According to query_posts you could do a filtering function like:

function filter_where( $where = '' ) {
    // where post_date > today
    $where .= " AND post_date >= '" . date('Y-m-d') . "'"; 
    return $where;
}
add_filter( 'posts_where', 'filter_where' );

And then your query_posts doesn't need to have post_status

OTHER TIPS

first change you query_posts(...) to this:

$args = array(
        'cat' => 4,
        'posts_per_page' => 6,
        'orderby' => 'date',
        'order' => 'DESC',
        'post_status' =>array('future','published'));
query_posts($args);

then add this code to your theme's functions.php file

function my_filter_where( $where = '' ) {

    global $wp_query;

    if (is_array($wp_query->query_vars['post_status'])){

        if (in_array('future',$wp_query->query_vars['post_status']){
        // posts today into the future
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
        }
    }
    return $where;
}
add_filter( 'posts_where', 'my_filter_where' );

now this is assumming that this is the only query that includes future posts otherwise you need to add a conditional parameter to the query and check for it in the filter function.

I'm not sure if post_status=future&post_status=published is the intended way. I guess it's like with orderby=date whatever just separated with a space. What happens if you just let published out of the query? I guess that's why you might bring up old posts. So far i got the only idea i got the filter out not already published future posts is a 2step process. The following is not tested and won't work out of the box. It should just give you an idea on where to look into the $post object and how to possibly avoid displaying posts that don't fit into your query.

$future_posts = query_posts( 'cat=4&showposts=6&orderby=date&order=DESC&post_status=future' );
if ( $future_posts->have_posts() ) :
  while ( $future_posts->have_posts()) : 
    $future_posts->the_post();

    if ( $post['post_status'] !== 'published' ) :
       // do stuff
    endif;

  endwhile;
endif;
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top