Wordpress 在wp查询中仅列出特定期间的帖子

Wordpress 在wp查询中仅列出特定期间的帖子,wordpress,wordpress-theming,Wordpress,Wordpress Theming,在WordPress上,我怎样才能做到这一点 限制wp查询仅列出最近一年(365天)内的帖子,不要列出超过365天的帖子 有没有这个插件?请告知。 <?php $date1yrback = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " - 365 day")); // Find Date 1 year back $countp = 0; if ( have_posts() ) : while ( have_posts() )

在WordPress上,我怎样才能做到这一点

限制wp查询仅列出最近一年(365天)内的帖子,不要列出超过365天的帖子

有没有这个插件?请告知。


<?php 

$date1yrback = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " - 365 day")); // Find Date 1 year back

$countp = 0;

if ( have_posts() ) : while ( have_posts() ) : the_post();

$my_date = the_date('Y-m-d', FALSE);    //Find Post Date

if($my_date > $date1yrback)             //Check if Post date is greater than date year back
{
    echo "<h1>".the_title()."</h2>";
    echo "<p>".the_content()."</p>";
    $countp++;                          //Increment counter as post in above criteria is found
}

endwhile; endif; 

if($countp == 0)
{
    ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php 
}

?>


在codex上有一个例子,展示了如何列出过去30天的帖子:

您应该调整它以满足您的需要,例如(未经测试):


您不需要插件,请看这里:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-1 year')) . "'";
    return $where;
}

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