有时候我们需要获取当天或者本周或者其他时间段内的文章更新数量,这时候使用 wordpress 自带的 date_query 就非常方便了。

获取今日更新数量

function get_today_post_count(){
$date_query = array(
array(
‘after’=>’1 day ago’
)
);

$args = array(
‘post_type’ => ‘post’,
‘post_status’=>’publish’,
‘date_query’ => $date_query,
‘no_found_rows’ => true,
‘suppress_filters’ => true,
‘fields’=>’ids’,
‘posts_per_page’=>-1
);

$query = new WP_Query( $args );

return $query->post_count;
}

获取本周更新数量

function get_week_post_count(){
$date_query = array(
array(
‘after’=>’1 week ago’
)
);

$args = array(
‘post_type’ => ‘post’,
‘post_status’=>’publish’,
‘date_query’ => $date_query,
‘no_found_rows’ => true,
‘suppress_filters’ => true,
‘fields’=>’ids’,
‘posts_per_page’=>-1
);

$query = new WP_Query( $args );

return $query->post_count;
}

可以看出,语义化的 date_query 实在是太方便了。