0

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');
0
0

Your regex cannot work: $wp_rewrite->preg_index(3) doesn't match anything, because you capture only one part in '/([0-9]{4})/?$', not three.

Try to add the page parameter:

$pagination_base = $GLOBALS['wp_rewrite']->pagination_base;
$month_archive = array( 
    $myqueryslug . '/([0-9]{4})(' . $pagination_base . '/(\d+))?/?$' => 
        'index.php?year=' . $wp_rewrite->preg_index(1) 
        . '&post_type=' . $myqueryslug 
        . '&paged=' . $wp_rewrite->preg_index(3)
);

I haven't tested it, just see it as a hint for where to look.

1
  • Thanks for pointing me in the right direction. I guess it's obvious I have no idea what I'm doing. I'll experiment and see how far I can get.
    – NatalieMac
    Mar 27 '13 at 23:43
0

Here's the code that worked:

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.