Join Morten Rand-Hendriksen for an in-depth discussion in this video What are custom post types and taxonomies?, part of WordPress: Custom Post Types and Taxonomies.
- View Offline
Before we dive into the code, it's important that you understand what Custom Post Types + Taxonomies are. Out of the box, WordPress has several different Post types already defined. And the most common ones, the ones you interact with on a daily basis, are Posts and Pages. So let's take a look at what Posts and Pages are, because this'll be instructive when we create our own custom Post types. Posts are what you normally create on WordPress. They are normally organized in a reverse-chronological order, so the last post goes first in the index, and then you see the rest of the posts below.
They're non-hierarchical, meaning posts do not have parent-child relationships. Instead, the posts are organized based on the author that created them, the date they were published, and also using categories and tags. Every post must have a category and all posts can have tags. In contrast, pages are single individual items that can be displayed in parent/child relationships. And you usually see this in the main menu. Pages don't have index pages and they don't have categories and tags.
So the only way to get to them is through a direct link and that's why they usually appear in main menus. Posts are normally used for any kind of recently updated item, so, a news item, an article, or anything like that, whereas pages are used for static content that is rarely updated and is usually made to be available, to inform the visitor about what is going on on the site rather than provide the information from the site itself. When you create a custom post type, you can create a post type that acts either like a post or like a page.
And whichever one you choose, the new custom post type will inherit all the features of either posts or pages. If you set the custom post type to act like a post, it can be organized by categories, or tags, or any type of custom taxonomy, or a combination of all of these. Custom post types have separate index pages, and you can also create custom index templates and later in the course I'll show you how to do that. Custom post types can have all of, a combination of, or none of the standard features that you find in poster pages, and you can set this up when you create your custom post type, which we'll do later in the course.
And finally you can call in a custom post type from anywhere within your sight using a custom loop. So for example, you can create a new loop that displays three posts from a custom post type within your index page. And towards the end of the course I'll show you how to create these types of custom loops, and also how to intermingle custom post type content with irregular posts in the index page. We're also going to take a look at custom taxonomy. So it's important that you understand what a taxonomy is. The definition of taxonomy is a scheme of classification.
Basically this means a taxonomy is an organizational system, that allows you to relate one item to other similar items in a hierarchical or a non-hierarchical group. To give you a real life example, you can think of card catalogs at a library, which display all the books in the library in a taxonomy system, or, you can think of your own clothing closet. In your closet, you probably organize your clothes in a certain way. You have your jackets in one section, you have your pants in one section, your socks in one section, your skirts in one section, and so on, so that they're easy to find.
If you're looking for a sweater, you go to where all your sweaters are. These are hierarchical systems, or categories, a type of taxonomy. But if you look at your clothes, you'll find that every piece of clothing has a tag in the back or on the side, that tells you what kind of material it was made of, and who made it, and what size it is, and so on. And these can be considered tags, or non hierarchical taxonomies, because they relate the content to each other, but it's not related in a way you want to sort them by. You would never go into a closet, and then sort all your cotton clothes in one section, and all your wool in one section, but they're still related to one another.
In WordPress, you have two default taxonomies that you use all the time. The first one is Categories, which is mandatory. Every Post must have a category. Categories are displayed in hierarchical systems. So you can have parent and child relationships between and if you go to a parent category, you'll see all the posts underneath the parent category and also underneath any of the children of the parent category and finally categories are the main organizational method within WordPress. Tags on the other hand are optional, you don't have to use them and you can use them whenever you want, they're non hierarchical meaning one tag does not relate to another tag in any way, and they are a secondary organizational method, just like I said with your closet, the tags in your clothes are a secondary organizational method.
When you create custom taxonomies, you can create either hierarchical taxonomies like categories, or non hierarchical taxonomies like tags. These taxonomies can be applied to one or several post types and you can also make the custom taxonomies apply to your regular posts within your site. Custom taxonomies can have custom index and archive templates so you can display them differently, from how you would display the category and tag indexes. Custom taxonomies are usually displayed along side categories and other meta content when you look at a post and finally, custom taxonomies allow for more advanced organization of your content.
To give you a better idea of what you can use custom polls types in taxonomy for, let me show you a real life example of an application of these features. This is, a WordPress website called Ciao Bambino that we built that displays both regular posts under the editorial sections of the sites. So here you see regular posts, and also displays hotel reviews that are sorted based on a series of different custom taxonomies. For example, right now we're looking at the location taxonomy, you can see it up here in the URL, it says ciaobambino.com/location/caribbean.
In here, you can sort your content based on what location that a specific hotel was in, or based on what type of hotel we're looking at and so on. And if you go and look at a specific hotel, you're now looking at a custom post type. Here we have the standard content you would see in all posts. The title, the content and some images, but all the rest of the information here is displayed using either custom fields or custom taxonomies. So you can use the custom pools type to display far more information in a far more advanced way than you would if you were just displaying a regular post.
And now that you understand how these features work, we're ready to build our own custom post types and custom taxonomies.
- What are WordPress custom post types and taxonomies
- Creating and testing the plugin
- Creating posts with custom post types
- Building basic custom taxonomies
- Creating custom post type templates
- Including custom post types in the front page index