WordPress 3.1 Custom Post Type Archives

Written by on March 23, 2011 in WordPress Tutorials - 12 Comments

A few days ago, I told you about creating Monthly Custom Post Type Archives, today I want to show you how to utilize the new native archive capability built into WordPress 3.1.

In order for this tutorial to make any sense, you need to know how to create custom post types. If you do not, then I highly suggest reading Justin Tadlock’s great article about the topic.

Let’s presume that we have created a custom post type like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
add_action( 'init', 'create_books_post_type' );
 
function create_books_post_type() {
 
	register_post_type( 'books', array(
		'labels' => array(
			'name' => __('Books'),
			'singular_name' => __('Book')
			),
		'public' => true,
		'show_ui' => true,
		'rewrite' => array(
			'slug' => 'book',
			'with_front' => false
			)
	) );
 
}

WordPress 3.1 has introduced a new argument we can pass to our custom post type init function called has_archive. By including this argument, WordPress will create an archive page for the post type. So now our custom post type init looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
add_action( 'init', 'create_books_post_type' );
 
function create_books_post_type() {
 
	register_post_type( 'books', array(
		'labels' => array(
			'name' => __('Books'),
			'singular_name' => __('Book')
			),
		'public' => true,
		'show_ui' => true,
		'rewrite' => array(
			'slug' => 'book',
			'with_front' => false
			),
		'has_archive' => true
	) );
 
}

Now we can access our Books archive by going to site.com/book. The archive name is determined by the slug argument by default, but if we wish, we can change it by doing:

1
'has_archive' => 'book-list'

which would render as site.com/book-list.

By setting has_archive to true, or any custom value, you can also create your own template for the archive display. By default the page will display with archive.php, but you can use your own custom template by creating a file with the name archive-{$post_type}.php. In this case, our post type name is books, so our archive file would be archive-books.php.

About

Pippin Williamson is an expert WordPress dev with 4+ years of experience. You may follow him on Twitter @pippinspages and @pippinsplugins and see his free WP plugins, themes, and tutorials he has to offer at Pippin's Pages.com and Pippin's Plugins.com

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

This works for me. A question:how can I add a "taxonomy filter" ? ie: site.com/book-list/CATEGORY_BASE/rowlingThanks 

pippinsplugins 7 pts

You have to first register your custom taxonomy, then set the slug of the taxonomy to "book-list/TAXONOMY_NAME"

 pippinsplugins  Thanks for your answer, but this way it doesn't show just books associated with a TAXONOMY_NAME term. It show also other Post Types associated with it (or at least the default Post Type "post"). As if the book-list/ parameter in the url is ignored. Here is the code I pasted in a brand new WP installation, theme Twentyeleven, functions.php, in the function twentyeleven_setup():http://pastebin.com/Y2FhpKSHSeems like the only solution for a POST_TYPE + TAXONOMY_TERM filter is something like this http://wordpress.org/support/topic/how-to-visit-a-taxonomy-archiveIf it's so...a lot of the advantage of having post types is missed...or...I'M MISSING something really important (something like :)Thanks for any help! 

pippinsplugins 7 pts

Matteo, neither of your links worked. Can you repost?

 pippinsplugins Sorry, some words sticked at the end of the url (same happened in my previous comment..."line break" beakes? :) ). Here they are:http://pastebin.com/Y2FhpKSH and http://wordpress.org/support/topic/how-to-visit-a-taxonomy-archive Anyway in the end I solved with this: http://wordpress.stackexchange.com/questions/43395/limit-taxonomy-results-to-a-single-cpt

 

pippinsplugins 7 pts

At least you got it figured out :)

pippinsplugins 7 pts

The has_archive parameter definitely still works. What do you guys have your "rewrite" parameter set to?

 

One thing to make sure of is that the slug of your post type is not the same as the slug for one of your pages.

Kevin Muldoon 24 pts moderator

Perhaps this doesn't work anymore due to updates to WordPress. pippinspages may know :)

My latest conversation: Automatically Publish Your WordPress Posts To Facebook and Twitter With SharePress

this is not working for me

Good article....
Now with the archive-{post_type}.php, we can customize the page.