如何显示每日/每周发布的帖子数?我不在乎缓存,我甚至可以禁用它。显然,如果有一种缓存方式,它会更有帮助。
显示每天发布的帖子数量
1 个回复
SO网友:Ismail
因为发布日期是存储的,所以您可以简单地对发布在给定间隔范围内的文章进行范围选择计数。
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 ) );
结束