Codeable info

WordPress 3.1 Custom Post Type Archives

Posted on by in WordPress Tutorials

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.

)
Codeable info

Comments (13)

Comment by danger_cris says:

Excellent!

Comment by pippinsplugins says:

At least you got it figured out :)

Comment by Matteo says:

[email protected], 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
 

Comment by pippinsplugins says:

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

Comment by Matteo says:

[email protected] 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! 

Comment by pippinsplugins says:

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

Comment by Matteo says:

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

Comment by pippinsplugins says:

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.

Comment by Kevin Muldoon says:

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

Comment by Ted says:

i also get a 404. any tips?

Comment by Dan says:

I get a 404. and I flushed.

Comment by Anjum says:

this is not working for me

Comment by Laxman Kasula says:

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

Codeable info