<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dev4Press&#187; post type archives Tag Archives, page 1 of 1 | Dev4Press</title>
	<atom:link href="http://www.dev4press.com/tag/post-type-archives/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dev4press.com</link>
	<description>Premium Plugins and Themes for WordPress</description>
	<lastBuildDate>Tue, 21 May 2013 19:59:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>URL Rewriting: Custom Post Types Date Archive</title>
		<link>http://www.dev4press.com/2012/tutorials/wordpress/practical/url-rewriting-custom-post-types-date-archive/</link>
		<comments>http://www.dev4press.com/2012/tutorials/wordpress/practical/url-rewriting-custom-post-types-date-archive/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 11:05:41 +0000</pubDate>
		<dc:creator>MillaN</dc:creator>
				<category><![CDATA[Practical]]></category>
		<category><![CDATA[archives]]></category>
		<category><![CDATA[custom post type]]></category>
		<category><![CDATA[date based archives]]></category>
		<category><![CDATA[Day archive]]></category>
		<category><![CDATA[GD CPT Tools]]></category>
		<category><![CDATA[new rules]]></category>
		<category><![CDATA[post type]]></category>
		<category><![CDATA[post type archives]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[rewrite features]]></category>
		<category><![CDATA[rewrite rule]]></category>
		<category><![CDATA[rewrite tab]]></category>
		<category><![CDATA[single posts]]></category>
		<category><![CDATA[WP rules]]></category>
		<category><![CDATA[Year archive]]></category>

		<guid isPermaLink="false">http://www.dev4press.com/?p=15392</guid>
		<description><![CDATA[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.]]></description>
				<content:encoded><![CDATA[<p>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.</p>
<blockquote><p>Before we go on, here is the previous article on rewriting:<br />
<a title="How WordPress URL rewriting works?" href="http://www.dev4press.com/2012/tutorials/wordpress/practical/how-wordpress-url-rewriting-works/">How WordPress URL rewriting works?</a></p></blockquote>
<p>Date based archives by default, work only with the default posts post types, and they look like this:</p>
<blockquote><p>Year archive: http://www.example.com/2011/<br />
Month archive: http://www.example.com/2010/10/<br />
Day archive: http://www.example.com/2012/02/22/<br />
Month archive, feed: http://www.example.com/2010/10/feed/<br />
Year archive, page 2: http://www.example.com/2012/page/2/</p></blockquote>
<p>But, to get similar URL&#8217;s for the custom post types, will not work. Also, each of theses URL&#8217;s supports pages and feeds as you can see on the last two examples.</p>
<p>If we have post type names &#8216;movie&#8217;, and for the post type archive slug we have set &#8216;movies&#8217;, you expect to get these date based archives for it:</p>
<blockquote><p>Year archive: http://www.example.com/movies/2011/<br />
Month archive: http://www.example.com/movies/2010/10/<br />
Day archive, feed: http://www.example.com/movies/2012/02/22/feed/<br />
Year archive, page 5: http://www.example.com/movies/2011/page/5/</p></blockquote>
<p>Such URL&#8217;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&#8217;s more, there are two ways you can do it: using a plugin and doing it your self with custom coding.</p>
<h3>Using GD Custom Posts and Taxonomies Tools Pro</h3>
<p>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.</p>
<div id="attachment_16126" class="wp-caption alignright" style="width: 250px"><a title="GD CPTTools Pro: Enable Date Based Archives" href="http://cdn.dev4press.com/wp-content/uploads/2012/04/gdpcttools_detail_datearchives.png" rel="lightbox"><img class="wp-image-16126" title="GD CPTTools Pro: Enable Date Based Archives" src="http://cdn.dev4press.com/wp-content/uploads/2012/04/gdpcttools_detail_datearchives-300x133.png" alt="GD CPTTools Pro: Enable Date Based Archives" width="240" height="106" /></a><p class="wp-caption-text">GD CPTTools Pro: Enable Date Based Archives</p></div>
<p>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.</p>
<p>If you want to check the rest of this plugin features, you can do it from here:</p>
<div class="xss-infobox xss-infobox-color-green">GD CPT Tools Pro Own Website: <a href="http://www.gdcpttools.com/" target="_blank">www.gdcpttools.com</a><br />
GD CPT Tools Pro On Dev4Press: <a href="http://www.dev4press.com/gd-taxonomies-tools/" target="_blank">www.dev4press.com/gd-taxonomies-tools</a></div>
<h3>Do it yourself with example code</h3>
<p>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 &#8216; generate_rewrite_rules &#8216;:</p>
<pre class="brush: php; title: ; notranslate">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-&gt;rules = $rules + $wp_rewrite-&gt;rules;
  return $wp_rewrite;
}</pre>
<p>This code attached  &#8216;my_datearchives_rewrite_rules&#8217; function to &#8216;generate_rewrite_rules&#8217; action. On line 3, we call another function that will generate rules for a post type &#8216;movie&#8217;. 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.</p>
<pre class="brush: php; title: ; notranslate">function my_generate_date_archives($cpt, $wp_rewrite) {
  $rules = array();

  $post_type = get_post_type_object($cpt);
  $slug_archive = $post_type-&gt;has_archive;
  if ($slug_archive === false) return $rules;
  if ($slug_archive === true) {
    $slug_archive = $post_type-&gt;name;
  }

  $dates = array(
            array(
              'rule' =&gt; &quot;([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})&quot;,
              'vars' =&gt; array('year', 'monthnum', 'day')),
            array(
              'rule' =&gt; &quot;([0-9]{4})/([0-9]{1,2})&quot;,
              'vars' =&gt; array('year', 'monthnum')),
            array(
              'rule' =&gt; &quot;([0-9]{4})&quot;,
              'vars' =&gt; 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.= '&amp;'.$var.'='.$wp_rewrite-&gt;preg_index($i);
      $i++;
    }

    $rules[$rule.&quot;/?$&quot;] = $query;
    $rules[$rule.&quot;/feed/(feed|rdf|rss|rss2|atom)/?$&quot;] = $query.&quot;&amp;feed=&quot;.$wp_rewrite-&gt;preg_index($i);
    $rules[$rule.&quot;/(feed|rdf|rss|rss2|atom)/?$&quot;] = $query.&quot;&amp;feed=&quot;.$wp_rewrite-&gt;preg_index($i);
    $rules[$rule.&quot;/page/([0-9]{1,})/?$&quot;] = $query.&quot;&amp;paged=&quot;.$wp_rewrite-&gt;preg_index($i);
  }

  return $rules;
}</pre>
<p>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&#8217;t need to change. Let&#8217;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).</p>
<p>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.</p>
<p>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&#8217;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 &#8216; my_datearchives_rewrite_rules&#8217; function, merged with WP rules and ready to use.</p>
<p>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.</p>
<h3>Conclusion</h3>
<p>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&#8217;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.</p>
<script src="http://feeds.feedburner.com/~s/adsense@gdragon.info?i=http://www.dev4press.com/2012/tutorials/wordpress/practical/url-rewriting-custom-post-types-date-archive/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://www.dev4press.com/2012/tutorials/wordpress/practical/url-rewriting-custom-post-types-date-archive/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk: enhanced
Content Delivery Network via cdn.dev4press.com

 Served from: www.dev4press.com @ 2013-05-21 22:11:19 by W3 Total Cache -->