Here's the code I'm using to create yearly archives for a custom post type:
function myquery_custom_post_rewrite( $rewrite_rules ) {
$myqueryslug = 'customposttype';
$month_archive = array( $myqueryslug . '/([0-9]{4})/?$' => 'index.php?year=' . $wp_rewrite->preg_index(1) . '&post_type=' . $myqueryslug . '&paged=' . $wp_rewrite->preg_index(3));
$rewrite_rules = $month_archive + $rewrite_rules;
return $rewrite_rules;
}
add_filter('rewrite_rules_array', 'myquery_custom_post_rewrite');
This works okay, except for the pagination. Trying to get to page 2 or any page other than 1 always shows a 404 page. Here's the URL I'm trying to use to get to pages other than 1:
http://mysite.com/customposttype/year/page/2
With the slug for my custom post type for customposttype and a year, like 2012, for year.
What am I doing wrong?
=============================
UPDATE: I got it working! Just in case anyone else needs it, here's the code that got it working:
function myquery_custom_post_rewrite( $rewrite_rules ) {
$pagination_base = $GLOBALS['wp_rewrite']->pagination_base;
$myqueryslug = 'customposttype';
$year_archive = array(
$myqueryslug . '/([0-9]{4})/' . $pagination_base . '/?([0-9]{1,})/?$' => 'index.php?post_type=' . $myqueryslug . '&year=$matches[1]&paged=$matches[2]',
$myqueryslug . '/([0-9]{4})/?$' => 'index.php?post_type=' . $myqueryslug . '&year=$matches[1]',
);
$rewrite_rules = $year_archive + $rewrite_rules;
return $rewrite_rules;
}
add_filter('rewrite_rules_array', 'myquery_custom_post_rewrite');