[英]Wordpress - get posts by date range
Im trying to get a list of posts from WordPress for the last 7,30 and 365 days. 我试图获取过去7,30和365天中WordPress的帖子列表。
Here is the code im using: 这是即时通讯使用的代码:
$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 );
If I error_log the args array I can see the timestamp being set. 如果我error_log参数数组,我可以看到设置的时间戳。 But when I error_log the WP_Query object I can see the SQL query:
但是当我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
This query does not hold the pub_date date range I have set. 此查询不包含我设置的pub_date日期范围。 How can I achieve this?
我该如何实现?
Credit goes to MichaelH in this Thread on wp.org forums. 在wp.org论坛上的本主题中,对MichaelH表示感谢。 You could modify it, to fit your requirements.
您可以修改它以满足您的要求。
<?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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.