1

Is it possible to only show posts that have todays date or in the future? I don't want to show any posts that are in the past.

Also, I want the list to show posts that have a date in the future as in the CMS it shows as scheduled.

Here's my loop:

<div class="news-content" style="background-color:#feefe7!IMPORTANT;">
  <div class="page-title-content">
    <h2><?php echo the_title(); ?></h2>
  </div>
  <div class="news-content-inner">
    <?php $portfolioloop = new WP_Query(array(
    'paged'          => get_query_var('paged'),
    'post_type'      => 'news',
    'posts_per_page' => 4,
    'tax_query'      => array(
        array(
        'taxonomy' => 'news',
        'field'    => 'id',
        'terms'    => 51,
        ),
        ),
    )); ?>
    <?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
    <div class="news-item" onClick="location.href='<?php echo the_permalink(); ?>'">
      <h2><a style="color:#F45B11!IMPORTANT;" href="<?php echo the_permalink(); ?>"><?php echo the_time('d.m.Y'); ?> / <?php echo the_title(); ?></a></h2>
      <p class="news-page">
        <?php if (get_field('description') != "") { ?>
        <?php echo the_field('description'); ?>
        <?php } else { 
        $newscontent = get_the_content();
        $newscontent_str = strip_tags($newscontent, '');
        echo substr($newscontent_str,0,250) . "…";
        } ?>
      </p>
    </div>
    <?php endwhile; // end of the loop. ?>
    <p class="news-page" style="font-size:12px!IMPORTANT;"><?php echo $portfolioloop->post_count; ?> opportunities</p>
    <?php if (function_exists('wp_pagenavi')) {
    wp_pagenavi( array( 'query' => $portfolioloop ) ); } ?>
  </div>
</div>

3 Answers 3

1

Can't believe I didn't see this sooner, simply solved it with this in the query:

'post_status' => 'future'

So once published it just disappears from the list.

0

As described here, you could add an additional filter to your query.

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    $where .= " AND DATE(post_date) >= DATE(NOW())";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

In your case that means the following:

<?php
 // Create a new filtering function that will add our where clause to the query
 function filter_where( $where = '' ) {
    $where .= " AND DATE(post_date) >= DATE(NOW())";
    return $where;
 }

 add_filter( 'posts_where', 'filter_where' );
 $portfolioloop = new WP_Query(array(
    'paged'          => get_query_var('paged'),
    'post_type'      => 'news',
    'posts_per_page' => 4,
    'tax_query'      => array(
        array(
        'taxonomy' => 'news',
        'field'    => 'id',
        'terms'    => 51,
        ),
        ),
    )); 
 remove_filter( 'posts_where', 'filter_where' );
?>
2
  • Ok thanks. I put that in my template and it removed the past posts but how can I get round showing posts in the future?
    – Rob
    Commented Apr 27, 2012 at 10:58
  • In this question you can find a solution to include also the future posts: wordpress.stackexchange.com/questions/16794/…
    – Thomas
    Commented Apr 27, 2012 at 12:22
-3
$query = new WP_Query('m>='.time());
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
endwhile
endif;
2
  • How would I incorporate this into my current code?
    – Rob
    Commented Apr 27, 2012 at 11:04
  • 2
    As far as I'm aware m isn't a query variable Commented Aug 16, 2012 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.