WordPress Cuztom Helper now enables custom post type archives out of the box.  In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{post_type}.php .

I decided to add this feature because one, generally, wants to be able to view all the posts of a custom post type. So if you have a custom post type named ‘Acme Products’, you can view all the products of this type by visiting example.com/acme-products/ in the browser.

In WordPress, custom post type archive pages are not enabled by default when you register a post type. If we left it like this in WordPress Cuztom Helper, one would either have to create a page template manually


/* Template Name: Custom Post Type Archive */

//somewhere in the template file 
global $query_string; 
query_posts($query_string . "post_type=YOUR-CUSTOM-POST-TYPE&post_status=publish&posts_per_page=10"); 
if ( have_posts() ) : while ( have_posts() ) : the_post();

and then create a blank page, choose the template and publish it, which is a lot of unnecessary work just to view posts of a custom post type. Or one would set

'has_archive' => true

while registering a custom post type. When has_archive is set to true, the custom post type slug is used for the archive page by default, which is a little unintuitive because acme_products/ makes more sense than acme_product/ when you want to view more than one product of Acme Product post type (it’s an archive page and not a single page).

There is an additional benefit to this i.e. saving the trouble of having to deal with slugs manually. Instead of trying to figure out slug_to_view_all_acme_products, you simply register your Acme Product post type and this will be done for you. In exceptional cases, such as for a custom post type named Music or Country Music where it makes sense to use the post type slug as archive slug, you simply set

'has_archive' => true

and your archive will be accessible at example.com/music/ or example.com/country_music

And when you want to disable the archive, simply set

'has_archive' => false

One just doesn’t have to deal with slugs unless one wants to use something drastically different. Another advantage of using archive over writing a Page Template is that one doesn’t have to create an archive-{post_type}.php page unless one is using custom taxonomies or metaboxes. Simply, the existing archive.php file be used. One also doesn’t have to use query_posts unless absolutely required.

So, from now on, custom post type archives are turned on when registering Custom Post Types with WordPress Cuztom Helper, as the default behavior. There you go. Download WordPress-Cuztom-Helper and take your custom post types to the next level with archives.

3 thoughts on “Custom post type archives with WordPress Cuztom Helper

  1. The problem with the whole archives template thing is that with custom post types we are supposedly creating custom data, and data structures. So creating a custom post type named news that looks the same as your blog, and indeed defaults back to the default blog archive template, may as well be a catagory.

    Why custom page templates for custom post types are a lot more powerful is that they allow you to create a page template that contains “meta content” about your custom data and allows the users a lot more control if we customize the admin and front end for flexibility and ease of use around the new data.

  2. @Paul: How I understand it is that archive template is just a specific page template intended to serve the specific purpose of an archive. It can do whatever a page template can do with regards to any custom post type data.

    As an analogy, if one has a generic mold that can be tweaked to create different objects and a mold that can be used as is to create a certain object called ‘A’, one would use A’s mold to create object A rather than tweaking and using generic mold to create object A, instead.

    That being said, creating a custom post type certainly makes sense only when you need something that doesn’t fit in the context provided by the default ‘Post’ type. Defaulting to the already present archive.php in absence of a specific archive-post_type.php is just a given for the title and editor features of a custom post type.

  3. Hi guys,

    Nice to see you two found eachother. It happens to be, we all worked on Cuztom Helper.

    @Abhinav: Thanks for implemting this feature to Cuztom Helper. At first, I didn’t see the point, but now I see the advantage of this (little) feature.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.