If you are building WordPress websites for clients (or for yourself), one of the best ways you can make it easier for maintaining content is to create custom post types (CPT). This article will explain the concept of custom post types, what they are used for, and will look a little bit under the hood as to how they work.
What are Custom Post Types?
At a basic level, content on WordPress sites is comprised of pages and posts. Pages are created for when you want to present static information (text, images, videos, etc.) to your website visitors. Posts are created when you want to add blog articles, timely information, or to categorise your content in different ways. Posts can be considered as a repository for your blog content. You can add categories and tags to posts, which can be used to sort your content in different ways. Pages and Posts are listed separately in the WordPress administration dashboard.
Although pages and posts are split into different sections within WordPress, they are actually stored in the same database table in the backend. Let’s take a look how this works.
One of the standard database tables in any WordPress installation is named wp_posts. At first glance, it might appear that only general posts would be stored in this table. However, delve a little bit deeper and you will find that wp_posts stores a lot of different information about posts AND pages, differentiating them by their Post Type.
On any new fresh WordPress installation, a default post and page is created automatically. The post is named “Hello World!” and the page is named “Sample Page”.
If we take a look inside the wp_posts table, we can see how it stored in the database.
Not only are posts stored in this database table, but also content from pages, attachments, revisions and navigation menu items. How are they differentiated? Through the post_type field. When a custom post type is created, posts of that type are still stored in the wp_posts table, with the exception being they will have have a different post_type value.
What are Custom Post Types (CPT) Used for?
When building a WordPress website, it is good practice to create different post types for different types of content. For example, when you read a newspaper, you will know that it is divided into different sections (e.g. news, sports, weather, classified ads, finance, etc). You can similarly create separate sections for your website based on different post types to divide content and manage it separately.
WordPress uses templates to determine how posts are displayed on your site. You can create templates for different post types so that different types of content are displayed in their own unique style and layout.
Using custom post types makes it much easier to navigate and create new posts. You can create different menu items in your administration dashboard to view and add/modify/delete posts of that custom post type (see screenshot below).
One of the biggest benefits is the use of custom fields. On the post creation form, additional custom fields specific for that custom post type can be added to make it significantly easier for the website editor to create content. For example, if you wish to add an Event for a club website, you could create a custom post type for Events. When adding a new event on the website, you could have custom fields on the form where the editor can enter information such as Location, Date, Time, Maximum Number of People, Organiser, and so on. The data from these custom fields can then be displayed and styled separately from the main post content as desired.
How to Create a Custom Post Type
A custom post type can be created by either writing some code (writing a new plugin or adding to your theme’s functions.php file) or by using a plugin. The easiest way to get started creating a custom post type is by using a free plugin called Custom Post Type UI which takes care of the hard work for you.
After installing the plugin, you will notice a new menu item in the administration dashboard named CPT UI. To create a new custom post type, select CPT UI and then Add/Edit Post Types.
From the Add/Edit Post Types screen, you can enter the details for your desired custom post type. There are many settings which allow you to set up the custom post type to your exact requirements. Most of the settings should be self explanatory, but if you aren’t familiar with the concept of slugs in WordPress, they related to how your posts are queried. The slug will appear in the URL for your post. For example https://domainname/events/postname/ (‘events’ is the slug for our custom post type).
Custom post types can make administration of any WordPress websites much easier and should be considered in the design as part of any project.