今天有人问我WordPress 获取最近24小时发布文章数量怎么能,瑞课百度了下找到了以下WordPress 获取最近24小时发布文章数量代码并分享贴到网站来。获取最近24小时发布的文章数注:最近24小时 – 是从用户当前的时间算起,往前24小时,这个时间段发布的数量。不一定全部是今天,也有可能是昨天某个时间的。
/**
* [get_posts_count_from_last_24h 获取最近24小时内发布的文章数量]
* @param string $post_type [参数默认为 post 这个类型,你可以填写其他文章类型]
*/
function get_posts_count_from_last_24h($post_type ='post') {
global $wpdb;
$numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(ID) ".
"FROM {$wpdb->posts} ".
"WHERE ".
"post_status='publish' ".
"AND post_type= %s ".
"AND post_date> %s",
$post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
)
);
return $numposts;
}