Im trying to get a list of posts from WordPress for the last 7,30 and 365 days.
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. But when I error_log the WP_Query object I can see the SQL query:
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. How can I achieve this?
Credit goes to MichaelH in this Thread on wp.org forums. You could modify it, to fit your requirements.