本文目錄1獲取最近24小時發佈的文章數2獲取今天發佈的文章數
@80033041 朋友留言咨詢,如何獲取今天發佈的文章數量,搜索瞭一遍,發現老外朋友已經分享過這方面的方法瞭(原文見這裡),稍稍整理分享下。
獲取最近24小時發佈的文章數
註:最近24小時 – 是從用戶當前的時間算起,往前24小時,這個時間段發佈的數量。不一定全部是今天,也有可能是昨天某個時間的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * [get_posts_count_from_last_24h 獲取最近24小時內發佈的文章數量] * https://www.wpdaxue.com/count-posts-or-custom-post-types-from-last-24-hours-or-from-today.html * @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; } |
/**
* [get_posts_count_from_last_24h 獲取最近24小時內發佈的文章數量]
* https://www.wpdaxue.com/count-posts-or-custom-post-types-from-last-24-hours-or-from-today.html
* @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;
}
將上面的代碼添加到當前主題的 functions.php ,然後在你需要調用的地方使用下面的代碼即可:
1 |
<?php echo get_posts_count_from_last_24h(); ?> |
<?php echo get_posts_count_from_last_24h(); ?>
默認為“post”這個文章類型,如果你要調用其他文章類型,比如 book,可以這樣用:
1 |
<?php echo get_posts_count_from_last_24h('book'); ?> |
<?php echo get_posts_count_from_last_24h(‘book’); ?>
獲取今天發佈的文章數
註:今天 – 也就是當天0點-24點。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * [get_posts_count_from_today 獲取今天內發佈的文章數量] * https://www.wpdaxue.com/count-posts-or-custom-post-types-from-last-24-hours-or-from-today.html * @param string $post_type [參數默認為 post 這個類型,你可以填寫其他文章類型] */ 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; } |
/**
* [get_posts_count_from_today 獲取今天內發佈的文章數量]
* https://www.wpdaxue.com/count-posts-or-custom-post-types-from-last-24-hours-or-from-today.html
* @param string $post_type [參數默認為 post 這個類型,你可以填寫其他文章類型]
*/
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;
}
將上面的代碼添加到當前主題的 functions.php ,然後在你需要調用的地方使用下面的代碼即可:
1 |
<?php echo get_posts_count_from_today(); ?> |
<?php echo get_posts_count_from_today(); ?>
默認為“post”這個文章類型,如果你要調用其他文章類型,比如 book,可以這樣用:
1 |
<?php echo get_posts_count_from_today('book'); ?> |
<?php echo get_posts_count_from_today(‘book’); ?>
參考:http://wordpress.stackexchange.com/questions/106383/count-posts-or-custom-post-types-from-last-24-hours-or-from-today