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.

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

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
$query = new WP_Query('m>='.time());
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
endwhile
endif;
share|improve this answer
    
How would I incorporate this into my current code? –  Rob Apr 27 '12 at 11:04
2  
As far as I'm aware m isn't a query variable –  Stephen Harris Aug 16 '12 at 11:25

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' );
?>
share|improve this answer
    
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 Apr 27 '12 at 10:58
    
In this question you can find a solution to include also the future posts: wordpress.stackexchange.com/questions/16794/… –  Thomas Apr 27 '12 at 12:22

Your Answer

 
discard

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

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