Last month I have published and article about WordPress rewriting as an intro to give better understanding what is happening behind the scenes when WordPress needs to resolve URL. This time, I will give you practical example to add date based archives to custom post types.

Before we go on, here is the previous article on rewriting:
How WordPress URL rewriting works?

Date based archives by default, work only with the default posts post types, and they look like this:

Year archive: http://www.example.com/2011/
Month archive: http://www.example.com/2010/10/
Day archive: http://www.example.com/2012/02/22/
Month archive, feed: http://www.example.com/2010/10/feed/
Year archive, page 2: http://www.example.com/2012/page/2/

But, to get similar URL’s for the custom post types, will not work. Also, each of theses URL’s supports pages and feeds as you can see on the last two examples.

If we have post type names ‘movie’, and for the post type archive slug we have set ‘movies’, you expect to get these date based archives for it:

Year archive: http://www.example.com/movies/2011/
Month archive: http://www.example.com/movies/2010/10/
Day archive, feed: http://www.example.com/movies/2012/02/22/feed/
Year archive, page 5: http://www.example.com/movies/2011/page/5/

Such URL’s will filter posts by post type and date, returning date based archives for a post type. But, it is not that hard to do this. What’s more, there are two ways you can do it: using a plugin and doing it your self with custom coding.

Using GD Custom Posts and Taxonomies Tools Pro

If you prefer to do things from the comfort of the admin interface, and if you need to do this for many post types, my GD CPT Tools Pro plugin is a best solution. You can add and manage any number of custom post types and you get long list of extra features plugin implements, including custom rewriting that includes date based archives.

GD CPTTools Pro: Enable Date Based Archives

GD CPTTools Pro: Enable Date Based Archives

For each custom post type, plugin includes many more rewrite options. On the rewrite tab, you can set up permalink structure for single posts, and you can control archives for post type. Simply enable the Date based archives checkbox (like on the image on the right), save post type, and plugin will generate rules for these archives.

If you want to check the rest of this plugin features, you can do it from here:

GD CPT Tools Pro Own Website: www.gdcpttools.com
GD CPT Tools Pro On Dev4Press: www.dev4press.com/gd-taxonomies-tools

Do it yourself with example code

If you prefer to do this yourself, here is the code that GD CPT Tools Pro uses for the date based archives. For the year, month and date archives you need one extra rewrite rule. And, if you want to have feeds and pages for them, you need 3 more rules for each one. So, in total there are 12 new rules for each post type for the date based archives. For this to work, we need to hook into action ‘ generate_rewrite_rules ‘:

add_action('generate_rewrite_rules', 'my_datearchives_rewrite_rules');

function my_datearchives_rewrite_rules($wp_rewrite) {
  $rules = my_generate_date_archives('movie', $wp_rewrite);
  $wp_rewrite->rules = $rules + $wp_rewrite->rules;
  return $wp_rewrite;
}

This code attachedĀ  ‘my_datearchives_rewrite_rules’ function to ‘generate_rewrite_rules’ action. On line 3, we call another function that will generate rules for a post type ‘movie’. We than add those rules to existing WP rules, and return rewrite object. Now we need the function that generates rules for specified post type.

function my_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;
}

To fully understand this code, you need to understand PHP regular expressions, but if you just want to use it, no worries, it is all here and this function doesn’t need to change. Let’s see what is going on in this function. Function is called with two arguments: name of the post type ($cpt) and the WP rewrite object ($wp_rewrite).

First, we need to find out what is the slug for the archives URL for this post type ($slug_archive). If you have disabled archives for post type, this function will exit on line 6. If the archives are set to use custom slug, function will use that, or it will use post type name (lines 7-9). Lines 11 to 21 are used to set up day, month and year based regular expressions rules and variables to be used in the query. First one is for day and it needs year, month and day, month based needs year and month, and year based only year. After that, we go through these 3 archives types and we generate rules for them. In the rule part we use regular expressions, and it uses ( and ) to separate capture groups. Each capture group provides a value for us to use, and each of these values need query variable in the query part of the rewrite rule.

Line 24 sets up the base for the query using post type name and on 25 we set up base for the rewrite rule using post type archives slug and the rule for the archive. Now, we need to add to the query all the variables needed for the rule (27 to 31). This builds the basic rewrite rule and rewrite query rule point to. Lines 33 to 36 are setting up rules for basic archive rule, feeds and pages. Feeds and pages need extra bits for the regular expression to detect feed or page, and extra query variable for feed or page. If you don’t want to have feeds for these date based archives, you can remove lines 34 and 35. Line 33 adds basic rewrite rule, and line 36 adds pages for archives, and you need to leave these two in place. After all the rules are generated, they are returned back to the rules in ‘ my_datearchives_rewrite_rules’ function, merged with WP rules and ready to use.

Whenever you make changes to rewrite rules, you need to instruct WordPress to rebuild the cached rules. To do this, it is enough to open Permalinks panel on the WordPress admin side under Settings. If you use GD CPT Tools Pro and its rewrite features, this plugin will flush rules on its own when needed.

Conclusion

Adding extra rewrite rules is not that complicated as you can see from this practical example. Depending on what you need, you need to learn (at least basics) PHP regular expressions and to make sure that query for the rule has sufficient number of variables to correspond to the number of capture groups in the regular expression. If you don’t want to lose time over this, and you need these extra rules, get yourself GD Custom Posts And Taxonomies Tools Pro and add new features with just a few clicks.

7 Responses to “URL Rewriting: Custom Post Types Date Archive”

  1. Danielle | April 30, 2012 at 8:00 am

    Thank-you this was exactly what I have been searching for.

    My date based archives are now working correctly.

  2. Brian Krogsgard | May 9, 2012 at 1:04 am

    Great post – very helpful, Millan. One thing I changed was how the slug is formed, so that instead of the name, it can use a rewritten slug if one exists: $slug_archive = $post_type->rewrite['slug'];

    Thanks again, I’ve got my CPT date archives all hooked up thanks to you!

    • MillaN | May 9, 2012 at 10:14 pm

      Thanks for stopping by. You are right, post type own defined slug can be used.

  3. Steven Gliebe | May 18, 2012 at 11:44 pm

    Thank you for showing how this can be done. When I learned that I could use an archive template for a custom post type I assumed that year/month/date would automatically work like the standard post archive. Not so!

    Thanks also to Brian for posting that bit. I was trying URLs using my rewrite slug instead of the post type slug and getting 404′s. I assume most people will use the rewrite slug.

  4. James | September 9, 2012 at 1:17 pm

    Cheers guys, I spent an age trying to get this to work, this solution worked for me! Although I was using CMS Press but it didn’t seem to work with that. Disabled that and registered the post-type in the normal fashion and it works perfectly.

    Cheers!

  5. Kurt | May 16, 2014 at 3:55 pm

    How would this work if you want to use it for more than one custom post type.

    I’ve adjusted the the following line to include my cpt ‘eagle’ but I also have a 2nd cpt called ‘gallery’ that I would also like to be able to sort by date.

    $rules = my_generate_date_archives(‘eagle’, $wp_rewrite);

    Also is it possible to use wp_get_archives to get a listing of the Monthly/Yearly Archive now that your rewrite script is in place?

    Thank You

    • MillaN | May 16, 2014 at 4:25 pm

      Hi, this tutorial is about adding rewrite rules, if you need custom functions to generate archives, you need to write them up. WP default wp_get_archives function can’t be used for this since WP doesn’t support date archives for post types, and you need new function for that.

Leave a Reply

Dev4Press Plugins Pack

Dev4Press Plugins Pack

Personal$99.00
Business$249.00
Developer$499.00
GD bbPress Toolbox

GD bbPress Toolbox

Personal$39.00
Business$99.00
Developer$199.00
GD Custom Posts And Taxonomies Tools

GD Custom Posts And Taxonomies Tools

Personal$39.00
Business$99.00
Developer$199.00
GD Press Tools

GD Press Tools

Personal$39.00
Business$99.00
Developer$199.00
GD Products Center

GD Products Center

Personal$39.00
Business$99.00
Developer$199.00
xScape Theme Club

xScape Theme Club

Standard$149.00