因为发布日期是存储的,所以您可以简单地对发布在给定间隔范围内的文章进行范围选择计数。
function count_posts_per_interval($seconds) {
global $wpdb;
$count = (array) $wpdb->get_row($wpdb->prepare(
"select count(ID) from wp_posts where post_status = \'publish\' and post_date between date_sub(now(), interval %d second) and now();",
$seconds
));
return (int) array_shift($count);
}
SQL查询可以
day
,
week
etc代替
second
, 但是既然WordPress附带了一些有用的
time constants, 让我们保持这样吧。
# Retrieve number of posts posted within last day
print_r( count_posts_per_interval( DAY_IN_SECONDS ) );
# Retrieve number of posts posted within last week
print_r( count_posts_per_interval( WEEK_IN_SECONDS ) );