{"id":787,"date":"2011-10-25T18:05:34","date_gmt":"2011-10-25T18:05:34","guid":{"rendered":"https:\/\/www.mohanjith.net\/blog\/?p=787"},"modified":"2011-10-25T18:29:04","modified_gmt":"2011-10-25T18:29:04","slug":"wordpress-how-to-monthlyyearly-and-daily-archives-for-custom-post-types","status":"publish","type":"post","link":"https:\/\/mohanjith.net\/blog\/2011\/10\/wordpress-how-to-monthlyyearly-and-daily-archives-for-custom-post-types.html","title":{"rendered":"WordPress How To: Monthly,Yearly and Daily Archives for Custom Post Types"},"content":{"rendered":"<p>For a plugin I was creating I needed Yearly and Monthly archives with my own slug. Even after setting the rewrite rules it was not working. After much debugging and several hours later I finally figured it out. I&#8217;ll explain how you could do the same bellow.<\/p>\n<p><strong>Spoiler Alert:<\/strong>\u00a0For the impatient, it was as simple as removing the old rules that are not setting the post type before adding the new rules \ud83d\ude42<\/p>\n<p>I&#8217;m going to assume that you have already familiar with Custom Post types. If you are not read http:\/\/codex.wordpress.org\/Post_Types and then come back. Most important are Step 5 and Step 6. Other steps are typical for any plugin or theme using custom post types.<\/p>\n<p>Step 1: Register the post type<br \/>\nRead more about registering custom post types at<br \/>\n[code lang=&#8217;php&#8217;]register_post_type( &#8216;my_class&#8217;,<br \/>\narray(<br \/>\n&#8216;public&#8217; => true,<br \/>\n&#8230;<br \/>\n&#8216;has_archive&#8217; => true,<br \/>\n)<br \/>\n);[\/code]<br \/>\nStep 2: Add rewrite tags and permalink structure<br \/>\n[code lang=&#8217;php&#8217;]$event_structure = &#8216;\/events\/%year%\/%monthnum%\/%my_class%&#8217;;<br \/>\n$wp_rewrite->add_rewrite_tag(&#8220;%my_class%&#8221;, &#8216;(.+?)&#8217;, &#8220;my_class=&#8221;);<br \/>\n$wp_rewrite->add_permastruct(&#8216;my_class&#8217;, $event_structure, false);[\/code]<br \/>\nStep 3: Add <code>post_type_link<\/code> and <code>rewrite_rules_array<\/code> filters<br \/>\n[code lang=&#8217;php&#8217;]add_filter(&#8216;rewrite_rules_array&#8217;, &#8216;my_class_add_rewrite_rules&#8217;);<br \/>\nadd_filter(&#8216;post_type_link&#8217;, &#8216;my_class_post_type_link&#8217;, 10, 3);[\/code]<br \/>\nStep 4: Return a proper permalink<br \/>\n[code lang=&#8217;php&#8217;]function my_class_post_type_link($permalink, $post_id, $leavename) {<br \/>\n$post = get_post($post_id);<\/p>\n<p>$rewritecode = array(<br \/>\n&#8216;%my_class%&#8217;,<br \/>\n&#8216;%year%&#8217;,<br \/>\n&#8216;%monthnum%&#8217;<br \/>\n);<\/p>\n<p>if ($post->post_type == &#8216;my_class&#8217; &#038;&#038; &#8221; != $permalink) {<\/p>\n<p>$ptype = get_post_type_object($post->post_type);<\/p>\n<p>$start = time();<br \/>\n$end = time();<\/p>\n<p>$meta = get_post_custom($post->ID);<br \/>\n\/\/ This is where I store when the class starts<br \/>\nif (isset($meta[&#8220;my_class_start&#8221;]) &#038;&#038; isset($meta[&#8220;my_class_start&#8221;][0])) {<br \/>\n$start = strtotime($meta[&#8220;my_class_start&#8221;][0]);<br \/>\n}<\/p>\n<p>$year = date(&#8216;Y&#8217;, $start);<br \/>\n$month = date(&#8216;m&#8217;, $end);<\/p>\n<p>$rewritereplace = array(<br \/>\n($post->post_name == &#8220;&#8221;)?$post->id:$post->post_name,<br \/>\n$year,<br \/>\n$month,<br \/>\n);<br \/>\n$permalink = str_replace($rewritecode, $rewritereplace, $permalink);<br \/>\n} else {<br \/>\n\/\/ if they&#8217;re not using the fancy permalink option<br \/>\n}<\/p>\n<p>return $permalink;<br \/>\n}[\/code]<br \/>\nStep 5: Add rewrite rules<br \/>\n[code lang=&#8217;php&#8217;]function my_class_add_rewrite_rules($rules){<br \/>\n$new_rules = array();<\/p>\n<p>\/\/ This is the important bit, unsetting the rules<br \/>\nunset($rules[&#8216;classes\/([0-9]{4})\/([0-9]{1,2})\/?$&#8217;]);<br \/>\nunset($rules[&#8216;classes\/([0-9]{4})\/?$&#8217;]);<\/p>\n<p>$new_rules[&#8216;classes\/([0-9]{4})\/?$&#8217;] = &#8216;index.php?year=$matches[1]&#038;post_type=incsub_event&#8217;;<br \/>\n$new_rules[&#8216;classes\/([0-9]{4})\/([0-9]{1,2})\/?$&#8217;] = &#8216;index.php?year=$matches[1]&#038;monthnum=$matches[2]&#038;post_type=incsub_event&#8217;;<br \/>\n$new_rules[&#8216;classes\/([0-9]{4})\/([0-9]{2})\/(.+?)\/?$&#8217;] = &#8216;index.php?year=$matches[1]&#038;monthnum=$matches[2]&#038;incsub_event=$matches[3]&#8217;;<\/p>\n<p>return array_merge($new_rules, $rules);<br \/>\n}[\/code]<br \/>\nStep 6: Don&#8217;t forget to flush<br \/>\n[code lang=&#8217;php&#8217;]add_action(&#8216;option_rewrite_rules&#8217;, &#8216;my_class_check_rewrite_rules&#8217;);[\/code]<br \/>\n[code lang=&#8217;php&#8217;]function my_class_check_rewrite_rules($value) {<br \/>\nglobal $wp_rewrite;<\/p>\n<p>\/\/prevent an infinite loop<br \/>\nif ( ! post_type_exists( &#8216;incsub_event&#8217; ) )<br \/>\nreturn;<\/p>\n<p>if (!is_array($value))<br \/>\n$value = array();<\/p>\n<p>$array_key = &#8216;events\/([0-9]{4})\/?$&#8217;;<br \/>\nif ( !array_key_exists($array_key, $value) ) {<br \/>\n$wp_rewrite->flush_rules();<br \/>\n}<br \/>\n$array_key = &#8216;events\/([0-9]{4})\/([0-9]{1,2})\/?$&#8217;;<br \/>\nif ( !array_key_exists($array_key, $value) ) {<br \/>\n$wp_rewrite->flush_rules();<br \/>\n}<br \/>\n$array_key = &#8216;events\/([0-9]{4})\/([0-9]{1,2})\/(.+?)\/?$&#8217;;<br \/>\nif ( !array_key_exists($array_key, $value) ) {<br \/>\n$wp_rewrite->flush_rules();<br \/>\n}<br \/>\n}[\/code]<br \/>\nThat&#8217;s it. Most important steps are Step 5 and Step 6<\/p>\n<div id=\"fb-like\" style=\"\"><iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https:\/\/mohanjith.net\/blog\/2011\/10\/wordpress-how-to-monthlyyearly-and-daily-archives-for-custom-post-types.html&amp;layout=standard&amp;show_faces=true&amp;width=300&amp;action=like&amp;font=&amp;colorscheme=light&amp;locale=en_US\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:300px; height:30px\"><\/iframe><\/div>","protected":false},"excerpt":{"rendered":"<p>For a plugin I was creating I needed Yearly and Monthly archives with my own slug. Even after setting the rewrite rules it was not working. After much debugging and several hours later I finally figured it out. I&#8217;ll explain how you could do the same bellow. Spoiler Alert:\u00a0For the impatient, it was as simple &#8230; <a title=\"WordPress How To: Monthly,Yearly and Daily Archives for Custom Post Types\" class=\"read-more\" href=\"https:\/\/mohanjith.net\/blog\/2011\/10\/wordpress-how-to-monthlyyearly-and-daily-archives-for-custom-post-types.html\" aria-label=\"More on WordPress How To: Monthly,Yearly and Daily Archives for Custom Post Types\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[292,126],"tags":[],"class_list":["post-787","post","type-post","status-publish","format-standard","hentry","category-tutorial","category-wordpress"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lUHm-cH","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/comments?post=787"}],"version-history":[{"count":8,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions"}],"predecessor-version":[{"id":796,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions\/796"}],"wp:attachment":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/media?parent=787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/categories?post=787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/tags?post=787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}