I’ve just released version 2.0.2. Once you upgrade to that you can add the following code to a custom plugin or your theme’s functions.php file:
add_filter('expanding_archives_get_posts', function(array $args) {
$args['cat'] = 2; // Replace with ID of your category.
return $args;
});
add_filter('expanding_archives_query', function(string $query) {
$category = get_category(2); // Replace with ID of your category.
if (! $category instanceof \WP_Term) {
return $query;
}
global $wpdb;
return "
SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year, COUNT(id) as post_count
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id AND {$wpdb->term_relationships}.term_taxonomy_id = 2)
WHERE post_status = 'publish'
AND post_date <= now()
AND post_type = 'post'
GROUP BY month, year
ORDER BY post_date DESC
";
});
Note that there are two places in the code where you need to set your category ID. Look for this:
// Replace with ID of your category.
The examples use category ID 2
. Replace the 2
with your own ID.
Also note that the results may not update instantly as the query to retrieve the date periods is cached for one day. To force the query to re-run, delete this transient: expanding_archives_months
You can either do that with PHP via: delete_transient('expanding_archives_months')
Or install a plugin like Transients Manager, which will allow you to search for expanding_archives_months
and delete it. (Then you can deactivate the plugin.)