WordPress does not produce pretty permalinks for a custom post type’s year archives, month archives or daily archives.
You can find a custom post type archive by using a query string ie: http://yourdomain.com/?post_type=reviews&year=2013&monthnum=09
.
You may want to rewrite your URL to http://yourdomain.com/reviews/2013/09
.
The following code snippet can be added to your WordPress theme’s functions.php file to create custom post type date archives that have pretty permalink structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/** * Custom post type date archives */ /** * Custom post type specific rewrite rules * @return wp_rewrite Rewrite rules handled by Wordpress */ function cpt_rewrite_rules($wp_rewrite) { $rules = cpt_generate_date_archives('[YOUR-CUSTOM-POST-TYPE]', $wp_rewrite); $wp_rewrite->rules = $rules + $wp_rewrite->rules; return $wp_rewrite; } add_action('generate_rewrite_rules', 'cpt_rewrite_rules'); /** * Generate date archive rewrite rules for a given custom post type * @param string $cpt slug of the custom post type * @return rules returns a set of rewrite rules for Wordpress to handle */ function cpt_generate_date_archives($cpt, $wp_rewrite) { $rules = array(); $post_type = get_post_type_object($cpt); $slug_archive = $post_type->has_archive; if ($slug_archive === false) return $rules; if ($slug_archive === true) { $slug_archive = $post_type->name; } $dates = array( array( 'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})", 'vars' => array('year', 'monthnum', 'day')), array( 'rule' => "([0-9]{4})/([0-9]{1,2})", 'vars' => array('year', 'monthnum')), array( 'rule' => "([0-9]{4})", 'vars' => array('year')) ); foreach ($dates as $data) { $query = 'index.php?post_type='.$cpt; $rule = $slug_archive.'/'.$data['rule']; $i = 1; foreach ($data['vars'] as $var) { $query.= '&'.$var.'='.$wp_rewrite->preg_index($i); $i++; } $rules[$rule."/?$"] = $query; $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i); $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i); $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i); } return $rules; } |
Remember to modify [YOUR-CUSTOM-POST-TYPE]
to reflect the post type you would like to create a date archive for.
Reference: Stackoverflow