In this tutorial we introduce one powerful WordPress capability: creating custom post type. We then introduce a very useful plugin for creating custom post type: Custom Post Type UI.
Why Custom Post Type?
WordPress stores content in various “default types”: post, page, attachment, etc. These are useful and sufficient for a blog site, but as a general content management system (CMS) it is too limited. Here wordpress’ custom post type provides the following benefits:
Naturally separating different content
For example, we might want to create a site showing various real estate houses and also real estate blog posts. We can still use the default ‘post’ type for both but it becomes messy to separate these two types of ‘posts’. It’s easier to simply declare a custom post type like “real_estate_housing” for real estate housing content and save the default “post” type for blog posts.
Combining with Custom Fields
Even more powerful of using custom post types is when they are combined with custom fields. Custom fields enable structured data for codes to easily recognize and process.
For example, a “real_estate_housing” custom post type can have two custom fields called “Latitude” and “Longitude” that store the location of the housing. This information can then be easily processed by map codes like Google Maps API to show a map displaying these housings’ locations.
Therefore, custom post types with custom fields gives us access to a widely used type of content: structured data, data defined by “key”->”value” pairs like “name”->”Bob Smith”, “email”->”bobSmith@xyz.com”.
Unlike default ‘post’ or ‘page’ type, structured data can be easily processed by codes to display it in drastically different ways: maps, infographics, etc. Structured data can also be used to create more powerful searches, like search by locations, prices or quantities.
We will explain more about combing custom post types with custom fields in later tutorials.
Declaring Custom Post Type
Unlike default post types, the default way to declare a custom post type is to add codes into your wordpress theme files, typically in
functions.php, using a function called
We won’t talk about this way of declaring post type since we have a better, graphical interface way of doing so: using Custom Post Type UI plugin.
Custom Post Type UI
The Custom Post Type UI enables us to add/edit/view custom posts directly from the admin panel.
Once the plugin is installed, a new table called “
CPT UI” will appear at the left admin tab. Click on it and it will show “Manage Post Types” page with add/edit/view/import&export tabs.
CPT UI main page.
At the “Add New Post Type” tab there are several sections below:
- Basic settings
- Additional Labels
We can left #2 unchanged for now. #1 are just basic settings name/slug settings like that for ACF’s field groups. #3 contains a lot of settings related to this post’s behaviors.
In many cases we don’t want custom posts to appear automatically, as we want to control how they are shown. Of the default setting in #3, only “Exclude from Search” should be changed to “True” so custom posts are not searchable by site viewers.
“Exclude from Search” setting in CPT UI.
Further below, one can choose “Supports” for this custom post and add Taxonomies (category and tags) just like normal post and page type. How to set these depends on the nature of your custom post.
Supports and Taxonomies settings.
Once you have finished your setting, click the “Add Post Type” button to save your custom post. You will see at the admin panel’s left column your custom post names appear. Then you can click them to add/edit individual custom posts.
The two highlighted tabs are my custom post types. I can click it and start adding custom posts.
We will explain some basic custom posts operations in the next tutorial.