WordPress 3.1 Introduces Custom Post Type Archives

While development on WordPress 3.1 is still ongoing and we’ll probably not see the final release until nearer the end of the year, I thought I’d let you know about a cool new addition to Custom Post Types that Andrew Nacin (Core Developer) worked on!

Little Background Info

Around 5 months ago #13818 was opened requesting an Index/Archive option for Custom Post Types because the way WordPress works meant that /post-type/ would just 404 on you although /post-type/wordpress-rocks/ worked just fine.

Similar could be said about Custom Taxonomies and how they function which I brought up back in February on the WP-Hackers Mailing List – I don’t think I was popular in the community that month for sure? While it’d be a nice addition I know many would like (and shut me up) I have been informed more than once it’ll not happen!

Now For Some Code

If you’re new to Custom Post Types and the register_post_type(); function then make sure to check out an in-depth article Justin Tadlock wrote on the subject, I recommend you read it anyway! :)

In our functions.php file lets setup a basic Projects Post Type with the Archive active.

add_action( 'init', 'mcw_projects_post_type' );

function mcw_projects_post_type() {

	register_post_type( 'projects', array(
		'labels' => array(
			'name' => __('Projects'),
			'singular_name' => __('Project')
			),
		'public' => true,
		'show_ui' => true,
		'rewrite' => array(
			'slug' => 'project',
			'with_front' => false
			),
		'has_archive' => true
	) );

}

We’re looking at the has_archive argument which has been set to true which will turn on the Archive for this particular Custom Post Type, normally it would default to false though! — But we can take that a step further because when it’s set to true it’ll fallback to the value in the slug argument. So here that would make it site.com/project/ which just wouldn’t look quite right!

'has_archive' => 'projects'

All we did was substitute true with projects which therefore shows site.com/projects/ as being home instead. Now we know we can do that, we can do all sorts of things (get creative), we don’t need to keep it in line with our Projects Post Type.

'has_archive' => 'work'

Thinking outside-the-box gives you site.com/work/ which is different, the option is yours!

What Else Might I Need To Know

A new archive-{$post_type}.php template was introduced although it will fallback to archive.php if it can’t find the appropriate template for the job. So for our Projects Post Type, as projects happens to be the value we used for the Custom Post Type, we’d call our template archive-projects.php — Similar to if we used the portfolio value for a Portfolio Post Type we’d end up calling our template archive-portfolio.php and so on.

If you get a 404 error when trying to view the Custom Post Type Archive then simply visit Settings > Permalink which will flush your rewrite rules for you, it’s as simple as that, and there we have it!

59 Responses

  1. Tom
    Tom October 26, 2010 at 9:42 PM |

    I have been waiting for how this would be implemented in ver 3.1… thanks for this. Are there plans for how someone might display a custom post type archive in the nav menu?

    Reply
  2. Michael Martin
    Michael Martin October 27, 2010 at 4:31 PM |

    Great post Mark. This should be a really nice addition for anyone looking to really get their value out of custom post types on a site!

    Reply
  3. Community Links: WordPress pumpkin edition | WPCandy
  4. WordPress 3.1 Beta Unleashed on WordPress.com « Lorelle on WordPress
  5. WordPress 3 | Blogging News, WordPress News, Social Media News from WordCast
  6. Rev. Voodoo
    Rev. Voodoo December 22, 2010 at 6:21 PM |

    I wonder if this will fix the issue I’ve been having. I use page templates for all my CPTs. Each querying their CPT. But each CPT page template has no way of knowing its relation to the home page I guess, so each thinks its the home page. This messes up a few things; breadcrumbs, all the CPT templates come up as home, but when you click home, you get to the real home; SEO plugins, all CPT templates get the SEO info of the home page, rather than their own; and Nav Menu, none of the CPT templates come up as current-menu-item, instead the Home page always highlights when on any of the CPTs.

    So hopefully this new feature clears that up!

    Reply
    1. Rev. Voodoo
      Rev. Voodoo December 27, 2010 at 4:48 PM |

      Well, I just put the RC1 on my dev install, and it fixes the issue with current-menu-item. So that’s the major issue resolved for me. I’m goping to wait and see if the plugin/breadcrumb/seo stuff resolves for the full release….when I upgrade my main install. I’m too lazy to install all the plugins on my dev install atm. Thanks for the write-up, it helped me get things going and understanding the archive function for my CPTs.

      Reply
  7. Weekly WordPress Review - WPCanada
    Weekly WordPress Review - WPCanada December 31, 2010 at 2:51 AM |
  8. rZr
    rZr December 31, 2010 at 12:54 PM |

    Excellent post, thanks. Much appreciated.

    Reply
  9. Custom Post Type Archives in WordPress 3.1 « i blog
  10. Daniel Pataki
    Daniel Pataki January 19, 2011 at 10:19 PM |

    It may be worth mentioning that you might have to flush your rewrite rules for this to take affect. I usually just go to the permalink settings and click on update without changing anything.

    Reply
  11. What’s new and exciting in WordPress 3.1 | WPCandy
  12. WordPress 3.1 Released | Mark McWilliams
    WordPress 3.1 Released | Mark McWilliams February 23, 2011 at 3:46 PM |
  13. WordPress 3.1 | WP TutsWP Tuts
    WordPress 3.1 | WP TutsWP Tuts February 23, 2011 at 5:03 PM |
  14. wpmag.com - WordPress News, Themes, Tutorials, Plugins, Questions, ...
  15. Custom Post Type Archives Part 4 | Adventures with Wordpress
  16. keitai
    keitai February 26, 2011 at 2:56 PM |

    Is there a way i can redirect custom post types to a subfolder in my theme? just like feature 3 from http://somadesign.ca/projects/smarter-custom-post-types/

    So if i have a custom post type named reviews, all i have to do is create a folder reviews with single.php , index.php and archive.php?

    Any tips, thoughts?

    Reply
  17. Marko
    Marko February 28, 2011 at 1:41 PM |

    Thanks for the info! What if template file is not working (recognizing) ?? I have template archive-myposttype.php but I get redirected to home page…

    Reply
  18. Steve Taylor
    Steve Taylor March 3, 2011 at 2:56 PM |

    Thanks for the post. I’m wondering, is there any interface available for including custom post type archives into nav menus? I can’t see one. Of course I can add a Custom Link, but I would have assumed that as you can add actual custom posts to nav menus, you’d also be able to add the archive pages.

    Reply
  19. jonesy
    jonesy March 22, 2011 at 8:35 PM |

    Hi Mark
    Thanks for this great post. But there’s somewhere that archive-posttype is failing (or I’m doing it wrong). Say I have a post type called product. And I have a custom taxonomy, heirarchical, containing, say, Fall, Spring, Summer. I’ve set up the rewrite rules so it goes to mysite/product/fall and mysite/product/sprint, etc. however, the archive template isn’t called on these custom taxonomy pages. So, mysite/product (the “archive” for the post type) correctly calls archive-product.php. But mysite/product/fall/ calls the normal archive.php file. This is unfortunate behavior; it seems that the template heirarchy is not respecting the post type and respecting instead the archive slug… Do you know if I’m doing something wrong or how I can fix this other than just checking within archive.php with many if/else statements (unfortunate and buggy)
    Thanks!

    Reply
    1. cfg
      cfg April 21, 2011 at 1:58 AM |

      Your rewrite rules sound like what I’m after…

      I’d be grateful if you could point me in the right direction for mysite/custom_post_type/custom_taxonomy …?

      Is there a ‘quick fix’ or not as such?

      Reply
  20. Umesh
    Umesh April 6, 2011 at 6:26 PM |

    Hi Mark,

    Thanks for the post, seems really promising but am not getting it work for me. I have a custom post(abc) with it’s own archive template (archive-abc.php) and default post for blog and am using wp_get_archives() function to list archives but the archive link directs me to the default post archives.

    It would be a great help if you could guide me through this.

    Thanks!

    Reply
  21. James Docherty
    James Docherty June 19, 2011 at 12:32 PM |

    Thanks for a great post Mark. I’d been struggling with getting the archive to work (didn’t want to resort to a plug in) but your clear instructions helped a great deal.

    Reply
  22. Darko
    Darko June 26, 2011 at 9:52 AM |

    Hi Mark, thanks for the article!

    I’m having some troubles with custom post type archives.

    Type is called ‘photo’ and ‘has_archive’ => ‘photography’. I have archive-photo.php, have flushed the rules, am using WP 3.1.1… and still, when I visit localsite/photography/ it returns 404.

    Also, how can I have taxonomy arhives? If tax is called ‘portfolio’, I want it to be accessible via localsite/portfolio/ but its not, 404 returned again. And I don’t really fancy creating a separate page template for that :)

    Thanks again :)

    Reply
  23. Nathan
    Nathan November 13, 2011 at 1:06 AM |

    Thank you! I spent forever trying to figure this out, seems like it should be default for the register_post_type function to create this page.

    Reply
  24. Dan
    Dan February 14, 2012 at 1:37 PM |

    Hi there,

    I’ve been trying to solve this issue for so long now…I have everything set as I think it all should be working. I have a custom archive page for my cpt, which lists the posts of the CPT, and custom template to display the cpt…all of this works, but I just cannot get the month archive to work.

    If my CPT archive page can be found at http://localhost/egi/testcpt/ then what should the URL be to show posts for January 2012, because http://localhost/egi/testcpt/2012/01 does not work, just returns 404….. PLEASE can somebody help me on this…driving mi nutz

    Reply
  25. Pavel Ciorici
    Pavel Ciorici April 14, 2012 at 8:04 AM |

    Thanks for great tutorial Mark, but do you have any ideas how I could link to the archive page with custom post types when Default structure of permalinks is enabled? I’ve tried everything I could, and nothing works: “mysite.com/?projects”.

    Reply
  26. Rich
    Rich October 24, 2012 at 11:29 AM |

    Any ideas on getting this to work with dates and pagination?

    For example:

    myurl.com/2012/10/my-custom-post-type/
    myurl.com/2012/10/my-custom-post-type/page/2/

    Thanks,
    Rich

    Reply
  27. Taulant
    Taulant February 12, 2013 at 11:09 PM |

    Hi Mark and thanks for this article, best one I could find on the topic.

    I was able to set up an archive page for my custom post type. By default it shows a list of all posts created under the post type., however I need to show only one page (which is the “homepage” for this section).

    I modified the archive-my-post-type.php template to only show that one post. However that one post exists on it’s own under http://www.mysite.com/customposttype/my-post-that-is-also-homepage/

    So now I solved the problem of having this on the homepage, but have the same content in two pages (bad SEO). I 301 redirected the post to the custom post type homepage. Hope this all makes sense!

    I’m wandering if this sounds like a good solution and / or if there is a better way to do this.

    Reply
    1. Taulant
      Taulant February 12, 2013 at 11:18 PM |

      In case I wasn’t clear enough :) …

      my goal is to have an “index” or “homepage” for custom post types, just what the article addresses, but this “index” page is not an archive (blogroll) page, but a single page created under this custom type. The issue with my solution is duplicate content (post is displayed in “index” but also it exists on its own). My current solution is 301 redirect the actual post.

      Reply
  28. Cam
    Cam April 5, 2013 at 5:52 PM |

    Excellent info,This should boost custom post types on a site!
    Thanks ,
    Cam

    Reply

Leave a Reply