Custom Post Type Archives in WordPress 3.1

When I released Prospress last year, it used a contrived system for creating an index of auctions.

This was necessary as WordPress provided no standard method for creating an index page for custom post types. Now that 3.1 is just around the corner, this is all about to change.

Creating a Custom Post Type Archive

Let’s get semantics out-of-the-way first. It may be called an archive, for reasons hotly debated, but an archive can act as an index for your post type, just like the index.php file for blog posts.

Now, let’s take a look at the two simple steps for using a custom post type archive.

Step 1: Add an Archive Template to your Theme’s Folder

Create a new template to act as your custom post type’s archive. You can use any markup or style in your template. I start by copying the theme’s existing index.php file and then add any additional template tags or make any style changes from there.

You must name your template archive-{post-type}.php and store it in the base of your theme’s directory. For example, the auctions archive for Prospress is archive-auctions.php.

You only need to use a standard loop in the template. No need to run a new query for your custom post type – WordPress takes care of all that.

Step 2: Tell WordPress Your Post Type has an Archive

Now we have our template, we need to tell WordPress we want it to use a custom archive.

When registering your post type, set the has_archive flag in the $args array to true. This tells WordPress to generate the rewrite rules for your post type’s archive page.

For Prospress, this looks like so (abridged & paraphrased):

$args = array(
		'public' 	=> true,
		'rewrite' 	=> array( 'slug' => 'auctions' ),
		'has_archive' => true,
		);
	register_post_type( 'auctions', $args );
}
add_action( 'init', 'register_auction_post_type' );

WordPress will now attempt to load archive-auctions.php for requests of the url example.com/auctions/.

A question on WordPress Answers also hints you can set the value of has_archive to create a custom rewrite instead of simply using true/false. I haven’t tested this though.

There you have it, a custom post type index or archive template. Told you it’s simple.

For further reading, check out:

Advertisements

About Brent

Born to code.
This entry was posted in Blogdex, WordPress and tagged , , . Bookmark the permalink.

5 Responses to Custom Post Type Archives in WordPress 3.1

  1. Thanks for including the link to my article! 🙂 — And I can confirm that setting has_archive without true (or false) and as 'wprocks' will result in the permalink being /wprocks/ just as the question on WordPress Answers hinted at! #allgood

    • Brent says:

      Cool thanks for confirming Mark.

      What use cases do you see for a custom permalink for the archive? To me it seems a little odd as you ou can create an inaccurate URL structure:

      For the index/archive page:
      example.com/wprocks/

      For a single page:
      example.com/custom-post/hello-custom-world/

      Unless the value of has_archive overrides the value of the rewrite slug? I might have to test that myself…

      • I think it’d vary in terms of how you’d use this specific feature, and the possible implications with going for a different URL structure. The only example I could think of at the time, would be the one I talked about in my article! — The option is there to use it at least though if you want?

  2. Pingback: Advanced Taxonomy Queries with Pretty URLs | i blog

  3. This design is wicked! You definitely know how to keep
    a reader amused. Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Excellent job.

    I really enjoyed what you had to say, and more than that, how you
    presented it. Too cool!

Comments are closed.