Custom Post Type Archives by Date with Custom Permalink

I’m a bit stuck with getting the custom permalink for custom post type (CPT) archive to work.

I’ve registered my ‘press_release’ CPT as following:

Read More
add_action('init', 'press_release_post_type_init');
function press_release_post_type_init() {
  $labels = array(
    'name' => _x('Press Releases', 'post type general name'),
    'singular_name' => _x('Press Release', 'post type singular name'),
    'add_new' => _x('Add New', 'Press Release'),
    'add_new_item' => __('Add New Press Release'),
    'edit_item' => __('Edit Press Release'),
    'new_item' => __('New Press Release'),
    'view_item' => __('View Press Release'),
    'search_items' => __('Search Press Releases'),
    'not_found' =>  __('No press release found'),
    'not_found_in_trash' => __('No press release found in Trash'), 
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'has_archive' => 'press-release',
    'rewrite' => array('slug' => 'press-release/%year%/%postname%','with_front' => false),
    'query_var' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'show_in_nav_menus' => false,
    'menu_position' => 20,
    'supports' => array(
      'title',
      'editor',
      'author',
      'revisions'
    )
  );
  register_post_type('press_release', $args);
}

At the moment if I go to URL http://mysite.press-release then I get to the archive-press_release.php template, which is correct.

However, I would like my CPT archive permalinks to be of the type press-release/%year%

I have tried to specify the above permalink structure when registering CPT:

'has_archive' => 'press-release/%year%'

However, with that permalink structure when I go to http://mysite.press-release or http://mysite.press-release/2011 in both cases I’m redirected to the index.php template not the archive one.

What do I add to make WordPress understand that press-release/%year% permalink with an optional year part are actually the archive templates?

I would really appreciate any help! I’m hoping it’s possible to do.

I was planning on constructing my own SQL in the archive-press_release.php to get the records of the correct year.

Many thanks,
Dasha

Related posts

Leave a Reply

1 comment

  1. Here’s the solution:

    1. Install Custom Post Type Archives plugin
    2. Go to Settings->Post Type Archives
      1. Remove the url base option (leave it blank)
      2. Change the template pattern to archive-{POST_TYPE}.php
      3. Tick your custom post type and click save
    3. Rewrite/save your custom permalink structure

    Now your links press-release/%year% and press-release/%year%/%month% should work.