有時候我們需要獲取當天或者本週或者其他時間段內的文章更新數量,這時候使用 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 實在是太方便了。