WordPress 获取今天/最近24小时发布的文章数量

时间:2019-08-29   阅读:715

获取最近24小时发布的文章数

注:最近24小时 – 是从用户当前的时间算起,往前24小时,这个时间段发布的数量。不一定全部是今天,也有可能是昨天某个时间的。

1
2
3
4
5
6
/**
 * [get_posts_count_from_last_24h 获取最近24小时内发布的文章数量]
 * https://www.boke.la/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
/**
 * [get_posts_count_from_today 获取今天内发布的文章数量]
 * https://www.boke.la/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

上一篇:WordPress 按首字母排列的标签云页面

下一篇:WordPress GIF 图片点击播放 WP GIF Player

网友评论