How to Create WordPress Custom Post Type
While WordPress can display a vast array of content of different types, organising them can be challenging. And since default options are limited, customising them can get tricky as well.
However, when you use custom post types, you have the option to create a new type of item such as pages and posts, which will come with a different set of data. Not only that, it will also have its own admin menu, custom taxonomies, editing pages, and other utilities.
If you are wondering what they are for, they are best for websites that have content that is organised along a structure that is unusual. In other words, if you want to display content differently than on regular pages and posts, a custom post type is exactly what you need.
A custom post type is also great for search engine optimisation (SEO), thanks to their built-in permalinks.
Custom Post Type: What Is It?
Despite its specific-sounding name, a post type can be used for any type of content. It is also likely that you have seen them before since developers utilise custom post types to add staff, testimonials, portfolios, and many others to their WordPress themes.
At its core, a custom post type is a regular post that has a different post_type value in the database.
There are 5 default post types:
- Navigation menu
WordPress 3.0+ gives users the capability to add their own custom ones.
The term taxonomy comes up often about custom types, and this confuses a lot of people. Simply put, taxonomies are a way to group custom post types and group posts together.
WordPress has four built-in ones:
- Link category
- Post formats
You also have the option to create custom taxonomies and use them in your post types to easily sort and group content.
Creating a Custom Post Type: How Should You Go About It?
Adding custom post types in WordPress is a breeze. Why? WordPress has included the core function register_post_type that can be used to create them. If you are a plugin developer, you can quickly include custom post types in the theme. You also have the option to add them via a custom plugin or via your child theme.
How to Manually Create a Custom Post Type
Prior to adding the custom post type manually, you need to answer a crucial question: where should you add the code? The ideal place to add and register your custom post types will depend on your project.
Case in point: if you are working on a client site that has an active theme, it would be best to create a child theme and register your post types from there.
If you are creating a custom theme, you can put the code in the functions.php file. If you are developing a plugin, where you add your code will not really matter if the code will run before the ‘init’ action hook to ensure it is available.
If the custom post type is vital, it would be a good idea to make it a must-use plugin. For those who are not familiar, must-use plugins can be found in a special directory in the content folder. They are also automatically enabled on all sites.
Custom post type is added to WordPress via the register_post_type( ) function. This will allow you to define a new one by multiple labels. Once you have created your header, you can use the function before the admin_menu, and after the after_setup_theme action hooks.
Given that it is created correctly, this can be pulled off using just a few lines of code.
Once done with the set up, you should be able to do the following:
- Add posts
- See the menu entry for the custom post type
- View the post list in the admit
- Visit them on your website
The next thing you need to do is create a 16×16 pixel icon image. Save it to your current plugin folder after. This is needed for the custom post type icon in the dashboard. Another option you have at your disposal is using a font icon. From there, you can activate the plugin.
A little note on naming: while it is convenient (and tempting!) to utilise a simple custom post type identifier, it is more ideal to prefix. Opt for a short namespace that will identify the theme, website, or plugin that uses the custom type.
How to Create a Custom Post Type Using a Plugin
Using a plugin is considered the easiest way to add new custom post types. The Post Types Unlimited plugin can make creation and management of custom post types a breeze. It also gives you the option to create custom taxonomies as well.
Start the process by installing the plugin. You can either grab it right from the WordPress directory or install it from your WordPress dashboard. You can find it under Plugin > Add New. Search for “post types unlimited.” Once you find it, install and then activate.
The process will add a new Post Types menu item at the bottom of your dashboard. Just click on it if you want to begin creating new post types and taxonomies. You can also find options you want to enable for your new post type. Just click the ones you want and press save. And that is it!
Post Types Unlimited was designed to work seamlessly with any WordPress theme. However, if you will use Total WordPress Theme, you will also get access to several powerful and exclusive options.
Creating post types can seem daunting at first glance. Fortunately, with a little practice, you will realise it is not as hard as most people perceive it to be. If anything, once you get the basics down, the rest should come easy!