只显示今天或将来的帖子(即不显示过去的帖子)?

时间:2012-04-27 作者:Rob

是否可以只显示今天或将来有日期的帖子?我不想显示任何过去的帖子。

此外,我希望列表显示的职位,有一个日期在未来的CMS中,它显示为预定日期。

这是我的循环:

<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 个回复
最合适的回答,由SO网友:Rob 整理而成

不敢相信我没有更早地看到这一点,在查询中简单地解决了它:

\'post_status\' => \'future\'

所以一旦发布,它就会从列表中消失。

SO网友:Thomas

如上所述here, 您可以向查询中添加其他筛选器。

// 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\' );
在您的情况下,这意味着:

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

SO网友:Mohit Bumb
$query = new WP_Query(\'m>=\'.time());
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
endwhile
endif;
结束

相关推荐

Thesis Theme Custom Loop

我正在使用论文主题构建一个网站,并使用论文自定义循环API和自定义WP\\U查询。当我在单页上执行此操作时,它不会显示评论表单。如何在单个帖子页面上添加评论表单