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

2020-06-20 0 338
资源下载
终身会员专享

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

2020-06-20资源编号:1274DIV CSS3380
如侵权、投诉或获取更新,请联系作者处理
文章目录

前些时在群里聊天,有个朋友在群里问: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


//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= %s ".
                "AND DATE_FORMAT(post_date, '%Y-%m-%d') = %s",
            $post_type, date('Y-m-d', time())
        )
    );
    return $numposts;
}

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


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


//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= %s ".
                "AND post_date> %s",
            $post_type, date('Y-m-d H:i:s', strtotime('-24 hours'))
        )
    );
    return $numposts;
}

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


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


 

文章来自小白学习资源站https://qyymz.com转载请注明出处,谢谢!!!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

千源源码站 DIV CSS WordPress获取本周/今日/24小时内更新的文章数量 https://qyymz.com/13850.html

常见问题

相关文章

猜你喜欢
发表评论
暂无评论