Custom post types and taxonomies

Creating custom post types and taxonomies

Transcript

Introduction

In this lesson, we will explore custom post types. By the end of this session, you should be able to describe a WordPress custom post type, understand why you might want to use one, generate a custom post type, and understand how to display your custom post type content on your website. So, what is a custom post type? The content you create on your WordPress site is stored in the database as pages, posts, and taxonomies such as categories and tags. Additionally, you can create custom post types and taxonomies to suit your specific needs.

Custom post types

Sometimes, your website may need something custom to enter and display content efficiently. So a custom post type is useful when creating content with a different format than a standard page or post. For instance, if you are a designer or photographer, you might want to create a portfolio post type to display your work. What might be some other examples of websites using custom post types? One of the most popular e-commerce plugins is WooCommerce. The products entered into and then displayed through WooCommerce are registered as custom post types and the product categories are custom taxonomies. Some other examples where custom post types might be appropriate could be for books, book reviews, team members, portfolio items, clubs, courses, lessons, and recipes; the list is endless.

So, what is the best way to establish a custom post type on your website? The general answer to that is to either write the code yourself. You can see the custom post type lesson in the developer pathway on this, or if you are not a developer use an available plugin that will set up a custom post type for you. And that will be the focus of this lesson. But before we continue, it is worth mentioning that the developer handbook says we recommend that you put custom post types in a plugin rather than a theme. This ensures that user content remains portable even if the theme is changed.

Creating a custom post type

Currently, the most popular plugins that can generate custom post types are Custom Post Type UI and Advanced Custom Fields or ACF. Other options may be found by searching for custom post types in the Plugin Directory. For this demonstration, we have chosen to use the Custom Post Type UI plugin. The process starts by installing and activating the CPT UI plugin. You will then see a new menu item on your dashboard, in this case at the bottom on the left column menu. Select “Add edit post types” and get started.

We will set up a custom post type on a hiking website that displays nearby mountains that can be climbed and classifies them by height and climbing ease. The process begins with naming your custom post type by designating a slug, namely mountain, and both single mountain and plural mountains labels. You will then be given a long list of labels associated with the new custom post type. There is a link available to populate the labels based on your custom post type slug. Click that option to save time and then scroll through the long list to see if you want to change any of the default labels. For instance, the default “Menu name” label adds the word “my” in front of the label. I prefer to remove that.

Then, we can scroll down to the settings area below all the labels. The settings establish such things as whether the custom post type is available to the public, whether it can be searched or appear in a navigation menu, where it appears in the dashboard menu list, and even what icon is displayed next to the menu item. You will also be able to designate what sections in the custom post type edit page are supported, such as featured image, excerpt, author, and others.

All of the setting items have default settings. You will want to change the “has archive” setting. Set that to true to have an archive page. If you think you might want to export the custom post type at some point, you can also set that item to true. Right at the bottom, you will see taxonomies to be assigned to the custom post type. The two displayed are the WordPress default categories and tags. We will be setting up custom taxonomies, so the defaults should not be selected. Once you complete those steps and save your designations, a new custom post type will appear in the WordPress dashboard’s left side menu along with the post and page menu items.

Custom taxonomies

Now you can designate any custom taxonomies that might help you more efficiently classify your new custom post type entries. In our case, we will classify the mountains using custom taxonomies called height and climb rating. Height and climbing ability would act as categories for our custom post type. We will generate several height ranges to segment the mountains. The same process will be used with the climb rating taxonomy by setting up climbing effort levels. To get started, select “Add edit taxonomies” in the menu area at the bottom left.

Then, we can start setting up the taxonomies in the same way that we established the custom post type. So, in the first example, I will call the taxonomy slug height, the plural label heights, and the singular label height. I will also select to auto-populate labels. Then, I will select the mountain custom post type as the post type to which the taxonomy applies. In the settings section, you will probably want to change two of the default settings. The first, Hierarchical, should be set to true if you want the taxonomies to be listed on the post type edit page. The other one is called “Show in quick or bulk edit panel” and setting this to true will allow you to change taxonomy settings of a specific mountain using the quick edit menu selection. Next, we can follow the same process for the climb rating taxonomy.

After establishing your taxonomies, in this case, height and climb rating, you must edit your mountain custom post type. So we will return to “Add edit post type”, select the mountain custom post type and then scroll down to the bottom of the settings section. Now you will see the height and climbing rating taxonomies listed. Check both of their boxes and save the custom post type again so that the taxonomies are related to that specific custom post type. Once you have set up your custom post type and its taxonomies, you can begin to populate them. When you select your custom post type in the dashboard, you can open up one of your taxonomies. In this case height and then the first classification would be mountains less than 3000 feet below sea level. Then, we can go ahead and add the other classifications for this taxonomy. Mountains less than 3000 feet below sea level up to mountains more than 12000 feet above sea level. Then, we can do exactly the same for the other taxonomy climb ratings. In this case, I’ve added a beginner, intermediate, and advanced classification.

Custom post type entries

Now that our taxonomies are set up, we can make a few custom post type entries. In this case, you would be entering specific mountains and selecting the taxonomies into which they fit. So let’s go ahead and create our first custom post type entry. I will name it first mountain and I’ll add some dummy content. And in our sidebar settings, we can apply the relevant taxonomies. And in this case, the mountain is less than 3000 feet and it is suitable for beginners. We will also add an excerpt as well as a post featured image. And then hit Publish. Then I will go ahead and create two more entries for the mountains custom post type, second mountain and third mountain. Now that you have your mountain post types entered and segmented by a height and climbing ease, you can provide your website visitors with a way to view the mountain list, either in its entirety or displayed by taxonomy.

Displaying your custom post type

First, we are going to generate a new page to be used to display the mountain custom post type archive. Name your page as you wish. In this case, we used mountains and then save it. Access the pages listing and select Quick Edit to display the page title and its slug. Change the slug to match the slug of your custom post type. In our case, that is mountain and then save the change. In order to see the page, we will need to add it to our menu in the header template part of the site. Access the Site Editor and select Navigation. Then Edit. Open the List View, select the Navigation block, add a new menu item, and select the page called Mountains. Then I will select Mountains in the right sidebar, click on the three vertical dots, add a sub-menu link, and type height. And as you can see, there’s no page called height. I only want to use height as a placeholder to display the classifications. So I will also add climbing rating as a placeholder and to ensure that it’s only a placeholder, you select the menu item, in this case height, open the sidebar settings and then add two hashtags below URL to ensure it’s not clickable, but merely a placeholder. We will do the same for the climb rating. We will add the sub-menu link as a placeholder and then add the two hashtags below the URL.

Then, we can go ahead and add the classifications as sub-pages to these placeholders. So let’s go ahead and select Height, click on the three vertical dots, click on Add a submenu link, we will search for the first classification less than 3000 feet, and select the taxonomy. Then, we can go ahead and add the classifications for both height and climbing ratings. Let’s go ahead and view it from the front end. When we click on Mountains, we will see all our custom post type entries, Mountain one, two and three. Then, we can select one of the classifications, in this case, mountains less than 3000 feet. For climbing ratings, we can look at mountains suitable for intermediate hikers.

Templates

The last thing we want to talk about is templates. All these pages that we’ve looked at are being displayed using the All Archives template. But what if you wanted one of your pages to have a different look and layout? If you make your way to the Site Editor and click on Manage all templates, we can click on Add new template top right. Then you have a few options. You can add the archive mountain template, which will display all your mountain entries. Or you can select the single item mountain template to change how single entries are displayed. Lastly, you can also modify how your taxonomies are displayed for all your taxonomies or single taxonomies or classifications in this case.

Conclusion

You can learn more about WordPress templates, there use and modification in some of our other sessions on learn.wordpress.org. All the best creating and setting up custom post types on your site.

Practical

Use WordPress Playground and re-create the example in the video, or create your own custom post types to test out your knowledge:

This is a preview lesson

Register or sign in to take this lesson.

Suggestions

Found a typo, grammar error or outdated screenshot? Contact us.