Skip to content
Advertisement

Limit wp_count_posts to the last 7 days

I want to display the total number of posts within a post type, but limit it to the last 7 days.

This is my current code successfully displaying the total amount of posts within the custom post type.

<?php $published_posts = wp_count_posts($type = 'games')->publish; echo $published_posts;?>

Is there a way by passing additional arguments? How would one go about this?

Advertisement

Answer

Unfortunately, wp_count_posts() isn’t going to get you there, it’s only going to give you a flat number of all of that post_type (and whether it’s readable or not).

The WP_Query class seems a bit intimidating at first, but it should get you exactly what you need since it has a lot more granular control built in, namely the Date Parameters.

After initializing WP_Query, you can use the found_posts property (NOT post_count!) to see how many posts it returned. Something like the following:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement