WordPress category archives by date
Most WordPress themes come with post archiving by category, and post archiving by date (year, month, day, etc.). The infrastructure for this is all built into the WP core.
But what if you want to show category archives by date?
You might have a category archive here:
http://yoursite.com/category/waffle/
And the archive for August 2009 here:
http://yoursite.com/2009/08/
But this URL won’t work “out of the box”:
http://yoursite.com/category/waffle/2009/08/
After a lot of time wasted with kludgy solutions, I found what seems to be the optimal solution thanks to Peter Swan on Snipplr:
function extend_date_archives_flush_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'extend_date_archives_flush_rewrite_rules');
function extend_date_archives_add_rewrite_rules($wp_rewrite) {
$rules = array();
$structures = array(
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
);
foreach( $structures as $s ){
$rules += $wp_rewrite->generate_rewrite_rules($s);
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'extend_date_archives_add_rewrite_rules');
Paste this into your theme’s functions.php file and it should work.
To understand what’s going on here, I found the Query Overview article on the WP Codex invaluable. This explains in plain English the nitty gritty of WP’s basic processing of requests, including the all-important (for this issue) WP->parse_request() method.
In the above code the definition of the $structures array is crucial. In the project I’m working on I’ve adapted that as I’ve got an extra little string (“/news/“) in my desired category/date archive link structure. I’m sure the method here can be the basis of a lot of time-saving; it certainly makes WP-as-CMS even more flexible.
UPDATE: OK, so I’m having some knock-on issues with the above code. It basically works, but its new rewrite rules are inteferring with some other aspects of the site. I’m still working on this, and I think my solution will end up being so site-specific that it won’t be that useful to post. However, I’ve found what appears to be Peter Swan’s Codex source for his hack, and I think this is where I’ll be learning how to massage the hack to work for me. Read all about Custom Archives.
Welcome! I build websites - mostly based on the brilliant, free & open 
Vincent (1st December 2009)
Thank you so much! I’ve been looking for this all morning, and it works for me!