Skip to navigation | Skip to content



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.

4 comments

  1. Vincent (1st December 2009)

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

  2. Mario Delgado (1st February 2011)

    This is the code i used for monthly navigation in the category.php, in addition to the code you quoted for the functions.php

    http://pastebin.com/jEEJTp25

  3. David Tomson (28th April 2011)

    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.

  4. Muay Thai (29th April 2011)

    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.

Comments on this post are closed.

Recent posts

View complete archives »