这里我为您编写了一个函数-
function the_dramatist_get_post_by_time_range( $args = \'\' ) {
$date_query = array();
if ( $args === \'last 7 days\') {
$date_query = array(
\'after\' => \'1 week ago\'
);
} else {
$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");
$date_query = array(
array(
\'after\' => $month_ini->format(\'Y-m-d\'),
\'before\' => $month_end->format(\'Y-m-d\'),
)
);
}
$params = array(
\'post_type\' => \'attachment\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
// Using the date_query to filter posts from last week
\'date_query\' => $date_query
);
return get_posts($params);
}
希望这会有帮助。它会把posts对象还给你。对于“过去7天”,请使用以下函数-
the_dramatist_get_post_by_time_range(\'last 7 days\')
对于“上个月”,按原样使用-
the_dramatist_get_post_by_time_range()
请先用不同的日期进行测试。如果你发现什么不对劲,请敲门。我认为日期范围可能会导致一些问题。所以请先测试一下。