当前位置:首页WP教程WordPress获取本周/今日/24小时内更新的文章数量

WordPress获取本周/今日/24小时内更新的文章数量

前些时在群里聊天,有个朋友在群里问:WordPress怎么获取到今日发布的文章数量,我嫌麻烦没有理会。今天在做wordpress博客文章统计的时候,自己刚好要用…

前些时在群里聊天,有个朋友在群里问:WordPress怎么获取到今日发布的文章数量,我嫌麻烦没有理会。

今天在做wordpress博客文章统计的时候,自己刚好要用到,于是在网上搜了搜,整理了一下几种文章统计的方式:

获取本周发布的文章数量

//WordPress获取本周发布的文章数量function get_week_post_count(){$date_query = array(array(\'after\'=>\'1 week ago\'));$args = array(\'post_type\' => \'post\',\'post_status\'=>\'publish\',\'date_query\' => $date_query,\'no_found_rows\' => true,\'suppress_filters\' => true,\'fields\'=>\'ids\',\'posts_per_page\'=>-1);$query = new WP_Query( $args );return $query->post_count;}

使用方法:

将上面获取本周发布的文章数量代码添加到当前主题的 functions.php

<?php echo get_week_post_count()?>

获取今天发布的文章数量

//WordPress获取今天更新的文章数量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= {5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s \".                \"AND DATE_FORMAT(post_date, \'{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}Y-{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}m-{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}d\') = {5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s\",            $post_type, date(\'Y-m-d\', time())        )    );    return $numposts;}

使用方法

将上面获取今天发布的文章数量代码添加到当前主题的 functions.php ,然后在需要调用的地方使用下面的代码即可:

<?php echo get_today_post_count()?>

上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:

<?php echo get_posts_count_from_today(\'site\'); ?>

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

//WordPress获取最近24小时发布的文章数量 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= {5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s \".                \"AND post_date> {5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s\",            $post_type, date(\'Y-m-d H:i:s\', strtotime(\'-24 hours\'))        )    );    return $numposts;}

使用方法

将上面获取最近24小发布文的章数量代码添加到当前主题的 functions.php ,然后在需要调用的地方使用下面的代码即可:

<?php echo get_posts_count_from_last_24h(); ?>

上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:

<?php echo get_posts_count_from_last_24h(\'site\'); ?>

给TA打赏
共{{data.count}}人
人已打赏
WP教程

WordPress纯代码实现给自定类型分类法投稿

2023-3-8 22:13:23

WP教程

WordPress纯代码添加文章投稿功能

2023-3-8 22:13:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
有新私信 私信列表
搜索