If I've got a custom publish type named 'my_type', how do i get Wordpress to create date-based archives, for instance:


I am searching for tips both on creating the rewrite rules as well as on connecting the web addresses to templates.


Update: I have attempted the next during my functions.php (EVENT_TYPE_SLUG is really a constant defined elsewhere):

function event_rewrite_rules() {
    global $wp_rewrite;
    $new_rules = array(
        EVENT_TYPE_SLUG."/([0-9]{4})/([0-9]{1,2})/$" => 'index.php?post_type=event&year='.$wp_rewrite->preg_index(1).'&monthnum='.$wp_rewrite->preg_index(2),
        EVENT_TYPE_SLUG."/([0-9]{4})/$" => 'index.php?post_type=event&year='.$wp_rewrite->preg_index(1),
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
add_action('generate_rewrite_rules', 'event_rewrite_rules');

the rewrite rules appear in $wordpress_rewrite-> rules, however when I navigate to individuals pages I recieve a 404 error. Basically by hand navigate to mysite.com/index.php?post_type=event&year=2010, I recieve rerouted to mysite.com/2010?post_type=event

Benj I believe WordPress instantly produces archives for custom publish type

OK ... required a while however i figured this out (there might be more elegant solutions available).

1) Produce a standard wordpress page for everyone because the archive page (and also to produce accessibility template). I defined the page's slug like a constant (EVENT_ARCHIVE_BASE) to ensure that it is simply hard-coded in one location and recommended otherwise.

2) Make custom rewrite rules that catch that page's web addresses and redirect these to the page:

function event_date_queryvars($qvars) {
    array_push($qvars, 'eyear', 'emonth');
    return $qvars;
add_filter('query_vars', 'event_date_queryvars');

function event_date_rewrite_rules() {
    // Adds rewrite rules for date-based event archives
    global $wp_rewrite;
    $new_rules = array(
        EVENT_ARCHIVE_BASE."/([0-9]{4})/([0-9]{1,2})/?$" =>
        EVENT_ARCHIVE_BASE."/([0-9]{4})/?$" => "index.php?pagename=".EVENT_ARCHIVE_BASE."&eyear=".$wp_rewrite->preg_index(1),
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
add_action('generate_rewrite_rules', 'event_date_rewrite_rules');

3) Towards the top of my page template, I look for the custom query vars and make the page accordingly. (I ought to happen to be able to utilize the built-in year and monthnum, but could not encourage them to work correctly. Future troubleshooting):

// Was the page called with a specific year, month, or just plain?
$year = get_query_var('eyear');
$monthnum = sprintf('%02d', get_query_var('emonth'));
if ($monthnum) {
    $list_type = 'Month';
    $monthname = $month[$monthnum];
    $heading = "Events from $monthname, $year";
} elseif ($year) {
    $list_type = 'Year';
    $heading = "Events from $year";
} else {
    $list_type = 'AllPast';
    $heading = get_the_title();

Just help, and hope this really is helpful for another person! Tell me for those who have a less complicatedOrconstructed-in method of doing this.

I handled to locate a more elegant/built-in solution.

The excitement is the "publish_type=article" parameter. Should you produce a rewrite like so:

'(articles)/([0-9]{4})' => 'index.php?post_type=article&eyear=' . $wp_rewrite->preg_index(2)

This can then keep the URL structure, but feel the default 'archive' template. Then you definitely steal web site from the default for your own archive template. (for example 'archive-articles.php')

This function achieves this (works best for me):

add_action("template_redirect", 'my_template_redirect');

// Template selection
function my_template_redirect()
    global $wp;
    global $wp_query;
    if ($wp->query_vars["post_type"] == "article")
        if (have_posts())
            include(TEMPLATEPATH . '/archive-articles.php');
            $wp_query->is_404 = true;

You will still have to make your own techniques to handle get_query_var()'s for that year/month/day, but a minimum of you don't have to produce a custom page template.