2012-10-04 29 views
0

在WordPress上,我该如何做到这一点?在wp查询中仅列出特定时段的帖子

将wp查询限制为只列出最近一年(365天)的帖子,并且不会列出大于365天的帖子。

有没有插件呢?请指教。

+2

你不需要的插件,看看这里:http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters – soju

+0

感谢这个作品 – Krunal

回答

3

有,说明如何从过去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
<?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 
} 

?> 
相关问题