How to Create a Custom Post Types Archive Page in WordPress

Posted on August 31st, 2010 by in Tutorials | 12 Comments  
How to Create a Custom Post Types Archive Page in WordPress

Custom Post Types was one of the awesome features included in WordPress 3.0. One of our users asked us a question on twitter (@wpbeginner), 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. So in this article, we will show you a step by step guide on creating a custom post types archive page in WordPress.

1. Creating a Custom Page Template

First step would be creating a custom page template in WordPress. All we will do is add the following code at the top of our new .php file which we will call (custompt-archives.php).

<?php /* Template Name: Custom Post Type Archive */ ?>

2. Adding the Necessary Template Codes

Next, you will need to add the necessary template codes including but not limited to header, footer, sidebar etc. This area will vary for each site, but for basics it would look like this:

<?php get_header(); ?>
This is where our Loop codes will go in the next step.
<?php get_sidebar(); ?>
<?php get_footer();?>

3. Creating our Custom Archive Loop

The loop is explained in details at the WordPress Codex, and we will be using different aspects of it to create our custom archives loop. Our loop will simply list posts with the necessary information surrounding it. You will be responsible for adding styling around it. Our loop:

<?php
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(); ?>

<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile;
endif; ?>
<div class="navigation">
	<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
	<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>

You can change the number of posts being displayed on the page by changing posts_per_page variable value.

Final Code

Your final code for the custom page should look like this:

<?php /* Template Name: Custom Post Type Archive */
get_header(); ?>

<?php
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(); ?>

<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile;
endif; ?>
<div class="navigation">
	<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
	<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>

<?php get_sidebar(); ?>
<?php get_footer();?>

Now upload this file in your theme’s directory. After you have done that, you need to go to your WordPress admin panel, and create a new page. Call it “Your Custom Post Type” Archives (or whatever you like to call it) and change the page template to “Custom Post Type Archive” as shown here. Do not add anything in the page content area, and simply publish the page. You should now have a custom post types archive page in WordPress.

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
phpadam 5 pts

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

katarsis20032002 5 pts

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/quer...

jmdocherty 5 pts

+1 for "does anyone know how to get the 'next' and 'previous' links working". Thanks for the great post though

RyanMauroDesign 5 pts

jmdocherty I'm looking for that answer as well. my 'next' and 'previous' links bring me to my 404 page.

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!

ValDes 5 pts

This really helped me out too. Any idea on how to get the prev and next links to work

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.

nice article but there is also a plugin out there caled: custom psot types archives: http://ratvars.com/custom-post-type-archives :-)

Tweets about us: