Steve Taylor photo

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_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.

UPDATE: I’ve removed the code that was at the start, which was flushing the permalink rules on every request. You should never do this. After installing the code, just go to Settings > Permalinks and press ‘Save Changes’. This should do the flush for you.

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.

8 comments

  1. Vincent avatar Vincent

    Thank you so much! I’ve been looking for this all morning, and it works for me!

  2. David Tomson avatar David Tomson

    Actually i am new to WordPress and i am trying to make it but it is very hard to understand because It basically works, but its new rewrite rules are interfering 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. Thank you for sharing this.

  3. Muay Thai avatar Muay Thai

    Your post is very helpful because I have just started making my own WordPress and your tips are great and useful! Thanks for sharing us. Hope you can post more about about WordPress like their plug-ins.

  4. This is from a while ago, but yes, I think that comment on WP forums is right. You shouldn’t flush the permalinks on every request. I’ll remove it and add a note – thanks!

  5. To be clear, I can’t really vouch for this code. It’s quite old, and the site I used it for is no longer active. Even at the time, as you can tell from the update at the bottom, I had some problems. Now, many WP versions down the line, there may be a better way to do it – I don’t know if the rewrite API has changed at all. It may still work OK, but probably best to Google around for a more recent solution.

Comments are closed.