2012-10-04 62 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 
} 

?> 
相關問題