2011-11-07 102 views
1

我試圖從過去7,30天和365天的WordPress的帖子列表。WordPress的 - 按日期範圍獲取帖子

下面是使用代碼IM:

$args = array(
     'posts_per_page'=>10, 
     'post_status'=>'publish', 
     'post_type'=>'post' 
    ); 
    if(isset($_GET['group'])){ 
     switch($_GET['group']){ 
      case 'week': 
       $time = date('Y-m-d h:i:s',strtotime('-1 week')); 
       break; 
      case 'month': 
       $time = date('Y-m-d h:i:s',strtotime('-1 month')); 
       break; 
      case 'year': 
       $time = date('Y-m-d h:i:s', strtotime('-1 year')); 
       break; 
      default: 
       $time = ''; 
       break; 
     } 
     if($time!=''){ 
      $args['pub_date'] = '>= "'.$time.'"'; 
     } 
    } 

    $query = new WP_Query($args); 

如果我是error_log args數組我可以看到的時間戳被設置。但是,當我的是error_log對象WP_Query我可以看到SQL查詢:

SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value desc LIMIT 0, 10 

此查詢不抱我已經設置了PUB_DATE日期範圍。我怎樣才能做到這一點?

回答

10

在wp.org論壇中,信譽轉到MichaelH this Thread。你可以修改它,以適應你的要求。

<?php 
    function filter_where($where = '') { 
    //posts in the last 30 days 
    //$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; 
    //posts 30 to 60 days old 
    //$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'"; 
    //posts for March 1 to March 15, 2009 
    $where .= " AND post_date >= '2009-03-01' AND post_date <= '2009-03-15'"; 
    return $where; 
    } 
add_filter('posts_where', 'filter_where'); 
query_posts($query_string); 
?>