iBegins
 

Share code hiển thị tổng số tin mới trong ngày WordPress

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;
}

Gắn code trên vào Functions.php rồi thực hiện chạy 2 dạng code sau:

echo get_posts_count_from_last_24h();
echo get_posts_count_from_today();

Chúc các bạn thành công, nhớ share bài viết thấy hay nhé.
Ngày 5 Tháng Bảy, 2019 ( 578 )

Tagged with: , ,

Bình luận bài viết

Đăng nhập để bình luận và xem các bình luận khác.