Creating Custom Content in WordPress: Custom Post Types
Creating Custom Content in WordPress: Custom Post Types
It’s pretty much universally agreed now that WordPress is more than just a blogging platform: It’s a Content Management System.
But in my view what makes it a powerful CMS is the ability to create custom content and display that content in exactly the way you need to.
In this, the first of of two posts I’ll look at the three types of custom content in WordPress and show you how to create that content. I’ll cover:
- Custom post types
- Custom taxonomies
- Custom fields.
In this first part of a two-part post series, I’ll introduce the concept of custom content in WordPress and demonstrate how to register and make use of a custom post type, as well as exploring how WordPress displays posts you create using that custom post type.
Understanding and Using Each Kind of Custom Content
Before you start creating custom content, it’s useful to know exactly what each one does and what scenarios you might use them in. Let’s start with custom post types.
Custom Post Types
WordPress comes with a set of post types available to you by default. These are:
- Navigation menu items
- Links (if you’re using the blogroll feature)
You can also create your own custom post types to store content which doesn’t fit into any of the above categories. Examples might include products in an e-commerce site or events in a listings site. If you’ve ever used a plugin to create a store, event listing or any other kind of unique content, the chances are you’ve created custom post types without even knowing it.
Plenty of our plugins use custom post types, for example:
- MarketPress uses a custom post type for products.
- CoursePress Pro uses custom post types for courses, instructors and students.
- Support System uses custom post types for tickets and FAQs.
There are plenty more examples, but this gives you an idea of the uses of custom post types.
It’s important to remember that custom post types are not posts. They don’t have any direct relation to posts and you should think of them separately. You can set up (or register) your custom post types to behave like posts or to behave like pages: it’s up to you. If they’re set up like posts you’ll be able to display archive pages of them while if they’re like pages they’ll be hierarchical. Most custom post types are configured to behave like posts but that doesn’t mean yours have to be.
Like custom post types, custom taxonomies add to the taxonomies that are already provided with WordPress. These are:
- Post formats
- Link categories (which apply to links in the blogroll if you’re using it)
You can also create your own custom taxonomies to organise your content more effectively. It’s important to remember that a custom taxonomy is not a category and has no relationship to categories: instead, ‘category’ is just one of the taxonomies used by WordPress.
Sometimes using the inbuilt categories will give you everything you need to sort your content, for example if you need to introduce a hierarchical category structure. But sometimes this won’t be enough, especially if you want to be able to display posts using more than one taxonomy, or if you’re using a custom post type and want to use a separate taxonomy for it.
- On an e-commerce site, product taxonomies – possibly multiple ones such as colour, size and fabric for a clothing store.
- On an events listings site, taxonomies for event type, venues and more.
- On a knowledgebase, taxonomies to sort data in a crosscutting way: for example if it’s a web development knowledgebase you might use taxonomies for programming languages, level of difficulty and platforms.
Again our plugins make use of custom taxonomies: for example in MarketPress there are product categories and product tags, which are separate from the categories and tags for blog posts; in CoursePress you have course categories; and in Support System there are taxonomies for ticket types and FAQ categories.
Before deciding if you need to register a custom taxonomy, you need to ask yourself if the existing category system will do what you need. If all you need is to be able to add a hierarchy to your categories, you can already do this.
So if I was developing a training site (for example) and wanted to use categories to sort posts by the skill or knowledge being taught, I might use hierarchical categories as shown in this screenshot. You can see here that I’ve got three top level categories: management development, personal development and technical skills, with everything else beneath these at the next level down.
But what if things got a bit more complicated? Let’s say I wanted to add a series of posts on communicating with your team.
It would make things easier if I could separate out the fact that these posts are for managers but also deal with communication skills. So maybe I could add a taxonomy for the audience, and use this to identify posts for managers instead of having management development as a category, I could create a new taxonomy for audiences. That way I can identify multiple topics and multiple audience groups for each post.
I’ll show you how to create a taxonomy like this in the next part of this series: once you’ve done it, you’ll be able to choose from both categories and audience groups in the post editing screen, as shown in the screenshot.
The third type of custom content is the custom field, also referred to as post metadata.
A custom field consists of two elements which you can see and edit in the post editing screen: the key and the value. You can use the same key again and again for multiple posts, but each will have a unique value. WordPress also gives each custom field you create its own unique ID, which means that each custom field for each post is unique even if they have the same key and value.
This is different from custom taxonomies: although you can choose from an existing key when creating a custom field, you can’t select from existing values you’ve used before. Generally this means that taxonomies are better for sorting and categorising data.
Note: Some plugins will give you the option to choose the value of a custom field from a dropdown box, but this will use a custom metabox created by the plugin in the post editing screen, instead of the standard custom fields interface provided by WordPress.
However there are some cases in which using a custom field can be useful to sort data, for example if you want to store numerical data. In an e-commerce site you don’t want to create a taxonomy for every possible price: instead you’d insert this in a custom field. You could then use this data to allow customers to identify products with a price below $20.00 for example, or to sort by price.
You can also use custom fields to store non-numerical data, meaning you can store and display similar data between posts and display it separately from the body of the post. For example in a jobs listing site you might use custom fields to store the location, salary and working hours for a vacancy.
This is what the Jobs & Experts plugin does: it uses a combination of custom fields (referred to as ‘Job Meta’) and taxonomies (‘Category and Skill’) to sort data:
Once you’ve added this data, visitors to your jobs site will be able to sort jobs by budget, display only those jobs which are still open and see the contact email and completion date for each job displayed prominently. A taxonomy is used for skills so that archive pages for each of these can be displayed.
You’ll learn how to create your own custom fields in the next part of this series: in this post, we’ll move on to creating custom post types.
Creating a Custom Post Type
Once you’ve identified that you need a custom post type, there are two ways to go about it: coding it yourself or using a plugin. I’ll demonstrate the code necessary to register a custom post type in a theme or plugin shortly but first let’s look at some of the plugins you can use:
- Our own CustomPress provides you with an interface for creating custom post types, taxonomies and custom fields and is very user-friendly. If you want to create many types of custom content, this will save you installing more than one plugin.
- The Custom Post Type UI plugin is the most popular free plugin for adding custom post types on the plugin repository. It lets you add custom post types and taxonomies, but not custom fields. The interface isn’t quite as user-friendly as with CustomPress, with more technical terminology for you to get your head round, but it does the job.
You can read more about plugins to help you create custom content in our review of the best CMS plugins.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!
But if you’re comfortable with coding your own, here’s how you go about registering a custom post type.
Coding Your Post Type
WordPress provides a function called
register_post_type() which you use to create your custom post type. You add the function to your theme’s functions file or (even better) to a plugin you create specially, and then fire it via the
init hook in WordPress.
Note: It’s better to register your custom post type in a plugin rather than your theme because then if you change your theme in the future, you won’t lose your custom post type.
To follow this technique you’ll need access to your WordPress files and a code editor. I recommend trying this out in a development site and not on your live site!
Start by creating a new empty file called
post-types.php and save it to the
plugins folder in your
Now add the following code to your file:
This opens the plugin file and tells WordPress that it’s a plugin. It also provides a title and a description which will help you identify the plugin when you activate it in your site.
Next, below the line that reads
*/ and above the closing
?> line, add these lines:
This creates a function which will hold the code to register your post type, and then attaches it to the
init hook using the
add_action() function. This makes sure that WordPress runs your function at the right time.
Now you need to add the code for your custom post type inside the curly braces:
Let’s take a close look at what this code does. The first section tells WordPress what labels you want to use for your post type. These will replace the default labels which are ‘Posts’, ‘Add Post’ etc. Next you call the
register_post_type() function, which has the following parameters:
title: your function won’t work without this.
labels: the labels you already specified.
has_archive: by setting this to
trueyou’re telling WordPress that it’s possible to show archive pages for this post type.
public: set this to true to make sure people can see a page on your website for each product.
supports: the elements of the WordPress admin that the custom post type supports.
taxonomies: an array of the taxonomies you want to use with the post type. I’ve used the inbuilt categories and tags, although you might want to leave this blank and register a custom taxonomy for your post type later on.
exclude_from_search: set this to false to make sure your products are included in search results.
capability_type: this is set as
postas our products will behave like posts and not pages.
rewrite: this tells WordPress what the slug is for the post type archive, which is the text to use after your domain name for its URL. By setting this to
products, the URL for the products page will be
Now save your file. The entire plugin’s code will look like this:
For your post type to work you’ll need to activate your new plugin. Go to the Plugins page in your site admin and find your new plugin:
Click the Activate link to activate it. You’ll now see an extra post type in your admin menu:
You can click on the Products link and start adding products to your site in just the same way as you would posts.
Displaying Your Post Type on the Front End
Once you’ve added some products, you’ll need to be able to display these on the front end of your site.
Setting Permalinks to Link to Your Post Type’s Pages
First you need to refresh the permalinks on your site so that WordPress will use the correct links to display products or product archives:
- In the WordPress admin, go to Settings -> Permalinks.
- Make sure the ‘post name’ option is selected.
- Click the Save Changes button.
Note: You need to do this even if you’ve already configured permalinks, as WordPress needs to refresh these settings for the new set of links to your products.
Adding Your Post Type to the Navigation Menu
If you want to add products to your navigation menu, you can. Go to Appearance -> Menus, and you’ll see that Products are displayed on the left. Simply drag any products onto your menu to include them.
Adding your product archive is a bit trickier, and for this you’ll need to add a custom link to the menu.
- Still on the Menus screen, click on the Links box to the left.
- In the URL field, type
mysite.comwith your own domain.
- In the Link text field, type Products.
- Click the Add to Menu button.
- Once the link has been added to the menu, move it to the right place and save the menu.
- Save your changes by clicking Save Menu. Don’t miss this step!
Now if you visit your site’s front end and click on that menu link, you’ll be take to your product archive page. Here’s mine:
So now you have a page to display all of your products. But what if you want to customize the way you display them?
A Template For Your Post Type
WordPress uses something called the template hierarchy to identify which template file in your theme it should use to display a given content type. It’s beyond the scope of this post to describe how the template hierarchy works in detail, but it helps to know how WordPress displays your new post type.
When WordPress displays a single product, it will look for each of these files in order in your theme:
- A file for displaying single products, called
- A file for deploying all single posts of any post type, called
- The generic file for deploying all kinds of content, called
When WordPress displays a product archive, it will work through these template files:
- A file for displaying the product archive, called
- A generic archive template, called
- The default template,
WordPress will work through this hierarchy and use the template file it comes across first. So if you want to create a custom template for your products, a good place to start is by copying the
single.php file, saving it as
single-product.php and editing that.
However if you don’t want to create template files, WordPress will happily use the files already present in your theme, which is what it’s done for me with the twenty fifteen theme’s
Being able to create custom content makes WordPress very powerful: you can uses it to add and display a range of flexible content types in whichever way you need to.
In this post you’ve learned about the three types of custom content, what they are and when to use each of them. You’ve also learned how to create a custom post type and display it on your site.
In the next part you’ll learn how to create custom taxonomies and custom fields.
Do you use custom post types in your sites? Do you prefer to use a plugin or code your own? What do you find them most useful for? Let us know your thoughts in the comments.