您可以编写自己的函数(将其添加到functions.php
文件:
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;
}
function get_posts_count_from_today($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 DATE_FORMAT(post_date, \'%Y-%m-%d\') = %s",
$post_type, date(\'Y-m-d\', time())
)
);
return $numposts;
}
然后在模板中使用它:
<?php echo get_posts_count_from_last_24h(); ?>
<?php echo get_posts_count_from_today(); ?>