Understanding WordPress CPT and Custom Taxonomy Archives

I spend a lot of time helping people with WordPress problems and most commonly, I encounter people trying to build a custom query to list all of the posts in their Custom Post Type (CPT). This is built off of the incorrect assumption that every single web page you want requires a WordPress “page” to be made in the dashboard. The following article is going to help you understand why you don’t need to do that.

First, understand what you are working with:

What is a Custom Post Type?

A Custom Post Type (commonly referred to as a CPT) is exactly what it sounds like. It is a customized “Post”. You can find the standard “posts” post type under your dashboard called “posts” and you can find a pre-made custom post type called “pages”. Blow your mind yet? Both posts and pages are considered two sides from the same coin! Most often the purpose of making a custom post type is to create a different UI for entering your new post type. Many times a web administrator will want different fields for their post type that they don’t care to have cluttering up their regular blog posts post type. For instance, if you write a blog you can use your “posts” post type but if you wanted to also have a list of books you are reading you can create a CPT called “books” and make a whole new set of text inputs for it like “author” or “copyright date” etc; you can also have a custom taxonomy specifically for it! (more on that later).

 What is a Custom Taxonomy?

A Custom Taxonomy is a category type basically. In default wordpress you have two taxonomies; one called ‘categories’ and one called ‘tags’. Both are ways to categorize your posts for users to more easily find related blog posts. Custom taxonomies can be used for all types of things but it is commonly used to create a unique set of categories for a CPT.

Let’s use the same previous example of having a CPT called “books”:

If you have a CPT called “books” that now has a separate UI for entering them from a blog post, you might want to create a separate set of categories for your books. Let’s say you read all types of books so you want to organize them by genre, you can use a custom taxonomy called “Genres” and in it have romance, fiction, non-fiction, horror, etc. The little categories” box will not show up in your “posts” UI because it has no need to be there, instead you can set it to only show up in your “books” CPT using the “object type” parameter for the register_taxonomy function.

How do I display a list of all posts in my ________?

Above was a brief overview of what the difference between some of the most commonly used and misused WP Objects are, but this is an article on the topic of how to query and display your CPTs and custom taxonomies. Let’s talk about that!

WordPress has an amazing thing built in to it. It can easily be seen when you view the Template Hierarchy but also can be easily missed if you have it in your head that you need to first make a “page” and then display your stuff on that page.

WordPress can create an “archive” page for every custom post type or every custom taxonomy you make automatically! 

Is your mind blown? Want to know how to do it with just one line of code?

One Answer: has_archive

When you create your custom post type with  <?php register_post_type( $post_type, $args ); ?> you should notice that under $args there is a parameter called “has_archive”. This will allow WordPress to create a page almost magically that you can browse to and see all posts in that Custom Post Type! Simply add  'has_archive' => true, to your $args and you will be able to go to yourdomain.com/[cpt-name].

To clarify:

I am going to register a post type called “Books” and I want to be able to go to domain.com/my-books and see a list of all of my CPT called “books” there. This is what I would do (simplified)

That is all I would need to do in order to see a list of all items in my CPT.

What About Custom Taxonomies?

Custom taxonomies are even easier! Custom taxonomies already have archives built in so you do not have to do anything for them to work! You simply have to go to domain.com/taxonomy-name/term

Let’s Clarify Again:

My above example had a taxonomy called “Genres” so my terms that I added via the WordPress dashboard would be ‘romance’, ‘horror’, and ‘non-fiction’. This means that I would go to domain.com/genres/romance and see a list of all posts under the “romance” genres.

That’s it. It’s that simple. You’re welcome!

Want to customize the way each new archive looks or want to display your custom taxonomies on your archive page? A new tutorial on how to do that is coming up! Tell us you want that in the comments and we will make it a priority!


About Lindsay

Self made millionaire.. Oops I mean web developer.. who started out learning how to build websites with tables and dreamweaver, I now almost entirely focus on CMS-based development, usually WordPress. I love coding and designing and regularly switch between my left and right brains.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">