Custom Post Types was one of the awesome features included in WordPress 3.0. One of our users asked us a question on twitter, how to create a custom post types archive page. We covered it in our initial article about Custom Post Types, but it wasn’t explained thoroughly. In this article, we will show you a step by step guide on how to create a custom post types archive page in WordPress.
First thing you need to make sure is that your custom post type has archive enabled in the code. To do this, you need to go to your custom post type code (can be found in your theme’s functions.php file or site-specific plugin file). You need to make sure that you have has_archive argument set to be true.
An example code would look like this:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'deals',
array(
'labels' => array(
'name' => __( 'Deals' ),
'singular_name' => __( 'Deal' )
),
'public' => true,
'has_archive' => true,
)
);
}
Notice how we have has_archive set to be true in the array. You need to have this in your code. Once you do that, your custom post type will be assigned an archive page which can be accessed by going to a link like this:
http://yoursite.com/deals/
Note: replace deals with whatever is the name of your custom post type.
WordPress by default uses the archive template of your theme to display the custom post type archive page. If you are ok with how the default archive looks, then you are done here. You have successfully created a custom post type archive page for your WordPress site.
However, if you want to create a custom archive page for your custom post type, then you would need to create a new file called archive-{posttype}.php. In our example, the file name would be archive-deals.php
The best way to start would be to copy the code from your theme’s archive.php file and paste it in your archive-{posttype}.php file. Then start tweaking from there. You can style this archive file to your heart’s desire. A very basic template would look like this:
<?php get_header(); if(have_posts()) : while(have_posts()) : the_post(); the_title(); echo '<div class="entry-content">'; the_content(); echo '</div>'; endwhile; endif; get_footer(); ?>
Once you are done upload this file in your theme’s directory. After you have done that, you would see a custom archive page for your custom post type.
There you have it. You should now have a custom post types archive page in WordPress. For those who want to see custom examples, then you can see those by visiting our WordPress coupons page or our WordPress books page.







Thanks a lot for this.
It’s really helpful for me to understand the flow of custom post type and how to handle it!
thanks again……
Hi,
Thanks for this tutorials.
Is possible to display, a Title and Description in a archive-post_type.php page?
I have lot of archive page in my site and I want to set a good SEO for this page.
I have create all Custom Post Type with a Custom post type UI plugin and with a Yoast plugin now I can set all SEO option in all custom post type but not in archive page.
Have a suggest for me?
Thank you
Lorenzo
Yes you should be able to add an h1 tag with title and description in the template.
Thank you!
I know this opportunity but I meant :
with the and code? how I can insert?
and
I have a network of blog for administrate multilanguage site (this site are not build by me, I had take this work of administration this site) and archive-post_type.php call a type of Custom Post Type that have the same name for EN site and DE site.
If add the code inside the archive-post_type.php, the Title and the Description are only in one language and I can’t insert SEO for DE site.
I know that my site is complicated and built bad but I can’t change this setting and I search a solution for insert SEO title, meta description and meta keyword in all languages that is made my site.
You think that is impossible for my settings?
Sorry but the html code that I’ve written are deleted from site.
in this paragraph : “with the and code? how I can insert?” I meant:
- with the Title and <meta description code
Use WPML plugin.
Not working in version 3.5.1 . I got 404 error.
Go to Settings » Permalinks. Then click save again. This should work then.
“Go to Settings » Permalinks. Then click save again. This should work then.”
It’s not working though I tried the way as you said. But same problem like IFTY. 404 error. I am using 3.5
To make pagination work you need to call the paged variable into your posts calls.
query_posts( ‘post_type=xxxxxxx&post_status=published&posts_per_page=10&paged=” . get_query_var(‘paged’) ′);
Works like a charm – thanks dude
I can’t get this code to work. In fact, I spent hours with it. I’m not sure what global $query_string actually does, but the code will not work while that is in there. No posts display at all. It’s as though the query doesn’t return any results.
If I replace this line: global $query_string; query_posts($query_string . “post_type=mentions&post_status=published&posts_per_page=10″);
with this line query_posts( ‘post_type=mentions&post_status=published&posts_per_page=2′);
I get results, but the pagination code does not work. The link changes from previous results to next results, but the result list does not change. The same posts are displayed on every page
@jmdocherty I’m looking for that answer as well. my ‘next’ and ‘previous’ links bring me to my 404 page.
the code above don’t work for me
the solution i was found there was in the codex
”
For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:
global $query_string; query_posts( $query_string . ‘&order=ASC’ );
When using query_posts() in this way, the quoted portion of the parameter must begin with an ampersand (&)…
”
the & is missing
this is the original link
http://codex.wordpress.org/Function_Reference/query_posts
+1 for “does anyone know how to get the ‘next’ and ‘previous’ links working”. Thanks for the great post though
This really helped me out too. Any idea on how to get the prev and next links to work
Thanks for the post, how ever — Your method was returning all products from all categories. I modified it to retrieve only the products in the current category.
$thisCat = get_category(get_query_var(‘cat’),false);
query_posts(“post_type=product&post_status=publish&posts_per_page=10&cat=”.$thisCat->cat_ID);
Hi ,
thanks for the tip!
although i didnt get it working, until i deleted the “$query_string .” from the query_posts string.
why is it in there?
thanks again
@sander
THANK YOU! for posting this! I was following the above tutorial and have been wracking my brain for several hours trying to get it to work. I decided to check the comments in case someone found anything and I got it working thanks to your comment!
same for me
@sander thank you
@wp-beginner: please edit the article and take out that damn $query_string thingy
@myself: check the wp codex before commenting.
“When using query_posts() in this way, the quoted portion of the parameter must begin with an ampersand (&). ”
and this is indeed the problem in the snippet provided above there is an ampersand missing:
query_posts(“&post_type
is the way to go..
further reading: http://codex.wordpress.org/Function_Reference/query_posts
This is a nice step.
For what I need? Half step.
I’d love to use this for, say, posts that are parents, and posts that are their children.
Catch is that a parent’s child can also have children, so that child is both child and parent. And if I can give that one both, I still have to keep the relationship straight … they aren’t apples and oranges in a barrel.
But thanks!
Interesting post, I will be trying this out in the near future. I have noticed the posts on this website really improving over the past couple of months, great job.
and a slightly simpler plugin with less features: http://wordpress.org/extend/plugins/simple-custom-post-type-archives/
nice article but there is also a plugin out there caled: custom psot types archives: http://ratvars.com/custom-post-type-archives