In today’s tutorial, we will explore one of the major features of WordPress i.e. ‘Custom Post Types’. Besides this, we will briefly learn about what is custom post types and how can we create it in WordPress.
WordPress is one of the popular CMS (Content Management Systems) that is known to provide adequate customization options within the platform. And it is the major reason, WordPress has been preferred by millions of people across the globe.
You can read the complete tutorial step by step on Hostinger.com
So let’s begin it :-
What is WordPress Custom Post Types?
WordPress is competent to hold and display a wide range of content types. In WordPress, a single content page is usually known as a post.
Some of the default post types in WordPress are Post, Page, attachment, etc. Although internally all kinds of post types are kept in the same place yet they are referred by particular post type. These are distinguished by a column, known as post_type.
You must see this : Add Custom Meta Boxes in WordPress Posts and Post Types
The latest version of WordPress (3.0) gives you the liberty to add your own custom post types. And by using them. you can sort posts on the basis of their content. In WordPress, generally, we create all our posts in the post sections and then they are given different categories according to content niche.
Opposed to this, custom post types support their own link within the admin control panel and leads to a list of posts of that type. However, they are assigned categories like a normal post, so that you can get the liberty to sort and display posts the way you like.
Look at the screenshot given below:
In custom post types, you can change various pieces of text. As you can see in the above screenshot, Add New Post is replaced with Add New Movie. According to your wish, you can replace ‘Featured image text’ with ‘Add Poster’
Let’s know how can we achieve Custom Post Types in WordPress.
Custom Post Types- Manual Creation:-
Although plugin is the most recommended way to create WordPress custom post types yet you can create WordPress Custom Post Types in a theme’s functions.php file.
For this tutorial, we will use the same movie database and review mentioned above.
Do you know : 5 Powerful Free Video Chat Plugins for WordPress
To create custom post type, you will have to write a new function i.e. register_post_type() first. Make sure your function is hooked to the init action hook or else custom post type created by you will not register properly.
See the following screenshot:
Make sure all custom functions are prefixed to avoid any conflict with other plugins.
The parameters for register_post_type()
- The name of the post type, characters limit is 20 characters. Moreover, it should not have any space or capital letter.
- An associative array is known as $args. It holds the information related to the post type in key => ‘value’ pairs.
Since both labels and arguments are both arrays, you should first write the labels in their own variable, then the $args variable and then call the function.
The $args Array:
Some of the most commonly used keys for $args array are listed below.
- labels – This array represents different pieces of text. For instance ‘Add New Post’ can be replaced with ‘Add New Movie’.
- Exclude_from_search- Whether the posts are available to appear in normal search results.
- Public- It defines whether the post is visible to authors only or public.
- Description- This array is used to write a short and descriptive summary of the post type.
Some other most use keys for $args array
- has_archive –
The labels Array:
The very first key of the$args array is known as labels. It is responsible to define different pieces of text associated with the post type. Since it contains enough information, most people create a variable known as $labels to hold it.
Here are some popular yet optional keys for the labels array.
- Menu name
- Add new
- Featured image
- Add new item, etc.
The Support’s Array:
This is very simple array, which allows you to write the list of post editor features that you are looking for your custom post type. By default, there are only two support’s array enabled i.e.
Here are some other post editor attributes you may enable in the $supports array:
- Revisions, etc.
Custom Post Types Creation Using Plugin
One of the most convenient ways to create a custom post type in WordPress is by using a plugin. This is the most recommended method for beginners as it is safe and easy as well.
For the same, first, you need to install and activate the Custom Post Type UI plugin. After activation, the plugin will add few new menu items in your WordPress admin menu, known as CPT UI.
Now you need to go through CPT UI->Add New to set up a new custom post type. See the screenshot given below.
This plugin gives you the liberty to develop custom taxonomies. And therefore, Add new custom post type page is parted into two columns. On the left column, you need to create your custom post type while the right column is for creating a custom taxonomy.
On the custom post type column, first, you will provide a name of your post type. As mentioned above it should not exceed character limit that is 20 characters.
- 2nd Field is ‘Label’- where you need to write the type of your custom post. For example ‘Movies’.
- The very next field is ‘Singular label’, where you have to describe the singular form for your label name, for instance ‘Movie’.
- The last field is ‘Description’- It should describe what your post is all about.
- That’s all, your custom post type is ready.
In this tutorial, we attempted to help you in learning how to create custom post types in WordPress. Hopefully, by using custom post types, you will add more features to your WP site.