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>
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' );
?>
  • 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
-3
$query = new WP_Query('m>='.time());
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
endwhile
endif;
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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