仅列出wp_query中特定时期的帖子

时间:2012-10-04 10:03:58

标签: wordpress wordpress-plugin wordpress-theming

在WordPress上我该如何完成这项工作?

将wp查询限制为仅列出最近一年(365天)的帖子,并且不列出超过365天的帖子。

这有插件吗?请指教。

2 个答案:

答案 0 :(得分:3)

在codex上有一个示例,显示如何列出过去30天的帖子: http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

您应该根据自己的需要进行调整,例如: (未经测试):

// 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' );

答案 1 :(得分:-1)

<?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 
}

?>