Only show posts with date of today or in the future (i.e don’t show past posts)?

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.

Read More

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>

Related posts

Leave a Reply

3 comments

  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.

  2. 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' );
    ?>