Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Create a Custom Post Types Archive Page in WordPress

Last updated on by
Elegant Themes
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, 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.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Davy

    Thanks, this is really helping me out!

    But I don’t get it the way I want it to. If I look at the examples given at the end, it looks quite similar to what I want to establish.

    I want to show 3 posts per row, with each post the title and thumbnail. When clicking on them there will be a pop-up window. (Html is ready for it so I only need to know how to set this up)

    This archive page is pretty much the same as I displayed my post in each category, but I’m getting stuck here..

    So how do I get this in to the php correctly?

    The page I made before, didn’t showed the posts but an link to archive….

    Can you help me out and tell me how the example pages work?
    They are quite similar to what I want.

    Thanks already,

    Davy

  • raul

    Hi there,

    Very helpful article. I’ll be trying it soon.
    I have a related question:

    Would you be able to somehow display the list of custom post archive pages, including not just the title of the pages, as you have it:
    • opinion
    • showcase
    • themes

    …but displaying also a featured image for each archive page, the way you can with post lists?

    • http://www.wpbeginner.com/ WPBeginner Support

      Yes sounds do able. One easier approach would be using featured images with the same name as custom post type then you can use something like:

      <?php
      
      $args = array(
         'public'   => true,
         '_builtin' => false
      );
      
      $output = 'names'; // names or objects, note names is the default
      $operator = 'and'; // 'and' or 'or'
      
      $post_types = get_post_types( $args, $output, $operator ); 
      
      foreach ( $post_types  as $post_type ) {
      
         echo '<img src="/path/to/images/' . $post_type . '.png" />';
      }
      
      ?>
      

      Needs improvement but we hope you get the idea.

  • Jamshed

    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……

  • Lorenzo

    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

    • http://www.wpbeginner.com Editorial Staff

      Yes you should be able to add an h1 tag with title and description in the template.

      • Lorenzo

        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?

        • Lorenzo

          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

        • http://www.wpbeginner.com Editorial Staff

          Use WPML plugin.

  • Ifty

    Not working in version 3.5.1 . I got 404 error.

    • http://www.wpbeginner.com Editorial Staff

      Go to Settings » Permalinks. Then click save again. This should work then.

      • Shahriar

        “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

      • Unknown Agent

        >Go to Settings » Permalinks. Then click save again. This should work then.

        Was also getting a 404 error and this solved it.

        Would never of fixed this other otherwise as such a massively illogical solution

        Thank you!

  • jim lott

    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’) ′);

    • Kyle

      Works like a charm – thanks dude

  • phpadam

    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

  • RyanMauroDesign

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

  • katarsis20032002

    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

  • jmdocherty

    +1 for “does anyone know how to get the ‘next’ and ‘previous’ links working”. Thanks for the great post though

  • ValDes

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

  • http://www.michaelbrentecklund.com/ Mike

    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);

  • sander

    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

    • http://cowgirlexpressions.com/ Ramsey Stoneburner

      @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!

    • Stephan Deglow

      same for me
      @sander thank you

      @wp-beginner: please edit the article and take out that damn $query_string thingy

      • Stephan Deglow

        @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

  • http://bentrem.sycks.net Ben Tremblay

    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!

  • http://www.darrnick.com Darren Nickerson

    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.

  • http://pacura.ru/ ovidiu

    and a slightly simpler plugin with less features: http://wordpress.org/extend/plugins/simple-custom-post-type-archives/

  • http://pacura.ru/ ovidiu

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