views:

35

answers:

2

If I have a custom post type named 'my_type', how can I get Wordpress to make date-based archives, for example:

mysite.com/my_type/2010/
mysite.com/my_type/2010/07/
mysite.com/my_type/2010/07/28/

I'm looking for tips both on creating the rewrite rules and on linking the urls to templates.

Thanks!


Update: I've tried the following in my functions.php (EVENT_TYPE_SLUG is 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 show up in $wp_rewrite-> rules, but when I navigate to those pages I get a 404 error. If I manually navigate to mysite.com/index.php?post_type=event&year=2010, I get redirected to mysite.com/2010?post_type=event

A: 

Benj I think WordPress automatically creates archives for custom post type

Mamaduka
If it does, I can't find them ...The following does exist: `mysite.com/2010/?post_type=event`, but that points to the standard archive page, and just filters based on event. It doesn't give me the url structure I'm looking for.
Benj
A: 

OK ... took some time but I figured this out (there may be more elegant solutions out there).

1) Create a standard wordpress page to serve as the archive page (and to give me access to the template). I defined the page's slug as a constant (EVENT_ARCHIVE_BASE) so that it's just hard-coded in one place and referenced everywhere else.

2) Make custom rewrite rules that catch that page's urls and redirect them 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})/?$" =>
            "index.php?pagename=".EVENT_ARCHIVE_BASE."&eyear=".$wp_rewrite->preg_index(1)."&emonth=".$wp_rewrite->preg_index(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) At the top of my page template, I check for the custom query vars and build the page accordingly. (I should have been able to use the built-in year and monthnum, but couldn't get them to work properly. 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();
}

Thanks for the help, and hope this is useful for someone else! Let me know if you have a simpler/built-in way to do this.

Benj