What is the Custom Post Type?
People often consider WordPress as just a platform for the blogging website but over the past few years, WordPress has evolved so much that it covers almost 30% of the web. Now, with the release of WordPress 5 and Gutenberg, WordPress is a complete robust content management system. WordPress comes with two default content types. One is called post and the other is pages. However, it allows you to create custom content type more often referred to as Custom Post Types. In this article, we will closely look for the importance of CPT and how to create a Custom Post Type with the plugin and without a plugin.
Importance of Custom Post Type:
Custom Post Type can be of much importance when you want to publish something which should be separate from posts and pages.
If you want to build a portfolio for your personal website that also includes blogs then what would be your approach to get the desired results? This is the place where the Custom Post Type comes. You just need to create a Custom Post Type for the portfolio. Having a separate section for the portfolio will allow you to easily manage the portfolio. This will also allow you to have custom fields and custom category structure.
How to Create a Custom Post Type?
There can be two ways to create a Custom Post Type in WordPress.
- Without Plugin
- With Plugin
Create a Custom Post Type without a Plugin:
There is no doubt that creating a custom post type without a plugin or manually may not be as simple as you would like it to be, especially when you are a beginner in Custom WordPress Development. But it can be helpful when you are not a big fan of using too many plugins. There is always a risk of deactivating plugin accidentally, which may cause the loss of CPT. And you may have to create CPT again. But, by creating CPT manually you will be able to make them permanent. This reason makes it worth trying to create manually especially when you have coding skills. Now let’s discuss how to create a CPT manually!
Register Custom Post Type:
Replace functions.php with the code below and save the file.
You will now see a new item Portfolio in the menu bar of your admin area in WordPress. This will work as a post but there is more to do. We have a few more steps to follow in order to get full out of our CPT. Now we will assign a template to our CPT.
Create Template for CPT:
To create a template for CPT follow these steps:
- Open the theme folder
- Create a new file named portfolio.php
- Add the below code right in portfolio.php and save the changes
All done now. You can now create a new portfolio using your own CPT. You have created the Custom Post Type. Now you can modify the code according to your requirements.
Create a Custom Post Type with Plugin:
If you are not an expert in Custom WordPress Development, creating a custom post type manually can be really hard to do but don’t worry. The nerds at the WordPress community know that not everybody is interested in doing customization themselves so, they have come with a number of plugins to make our lives easier. Here are some of the most used plugins for Custom Post Types:
- Custom Post Type UI
- Easy Content Types
- Custom Post Types Maker
We will be using Custom Post Type UI, in order to show you how to create a Custom Post Type with the help of a plugin.
Custom Post Type UI:
CPT UI provides you an easy way to register and manage all the custom post types and taxonomies of your website. By using this plugin, you can create new post types and their relative taxonomies. The best feature of this plugin is that it allows you to import and export post types with much ease. The best part is yet to come. IT IS FREE!
Now let’s see how to use Custom Post Type:
- Install the plugin.
- Activate the plugin
- A menu option will appear named CPT UI.
- Click add new Post type. Fill the fields with post slug, plural label, and singular label
Just go to CPT UI -> Add/Edit Post types
- Clicking on Add Post Type button will create the post type named Portfolio
- You can also apply different settings to post type and select support features for your post type:
- This will result in showing a post type in the menu bar in the WordPress admin area
- You can have the luxury of editing your post type any time. You just need to go to CPT UI -> add/edit post type -> edit post type. And edit the fields as shown in the image
- Next step is to add taxonomies for the post type. Just navigate to CPT UI -> Add/Edit Taxonomies.
- You can also apply different settings to taxonomy:
- Click on the add taxonomy and this will show you the taxonomy in a sub menu of post type Portfolio
Now it is all done. You can enjoy setting up Custom Post Types as many as you like which can surely enhance the maintenance and make it easier to publish new data.
But what if we need to add extra fields in our post type. There is a solution to every problem in this world. There are some plugins available in the WordPress marketplace for users to make custom fields. This becomes vital for Custom WordPress Development and to benefit from WordPress fully.
Plugins for Custom Fields:
Creating custom fields manually can be burdensome for developers. However, these plugins are helping developers to bypass this hurdle.
- Advanced Custom Fields
- Simple Fields
- Custom Fields suite
- Just Custom Fields
In this article, we will be discussing the most used plugin for custom fields, which is Advanced Custom Fields (ACF). It is available in free version as well as premium version.
How to add Custom Field in CPT:
Using ACF and CPT simultaneously is as simple as it can be. Follow the steps in order to add some custom fields in our CPT “Portfolio”:
- Install the ACF plugin from WordPress
- Activate your version. If you are using premium version then you will have to provide the activation key.
- Upon activating the ACF plugin successfully, you will be able to see.
- Click on Add New for a new set/group of fields. Enter the name and select post type for which you are trying to create custom fields. In our case, it is Portfolio.
- Click on Add Field button to add your first field.
- Upon clicking, some fields will appear. Complete the fields with your desired settings.
- We can add as many fields as we want by repeating step 6. In our case, we have created four custom fields for our portfolio post type.
- ACF provides us the functionality to add more than one field group for different post types. We can access the field groups under custom fields.
- You can edit any of the field group when needed from Field Groups.
- After completing this step, publish the field group from the right sidebar.
- Once the field group is published, you will see your custom fields in the post type we created earlier.
That’s it, we have completed creating Custom Post Types and Custom fields using Custom Post Type UI and Advanced Custom Fields respectively. But what if there is a possibility that you have to show the CPT in widgets area in the sidebar. There is a solution to this issue as well. The WordPress marketplace offers you a free plugin called Ultimate Posts Widget.
Display Custom Post Type in Widgets Area:
- Once you install and activate the plugin. Navigate to Appearance -> Widgets,
- Scroll down to find Ultimate Posts Widget.
- Select in which sidebar you want to show CPT.
- Go to the sidebar -> Ultimate Posts -> Filter, you will find a drop-down having all possible options.
- Select your desired option. In our case, it is Portfolios
- Save changes and you will be able to see portfolio widget in your sidebar.
Hopefully, this article has helped you in learning about the importance of Custom Post Type and how to create it with a plugin and without a plugin. Moreover, we also discussed how to use the Custom Post Type UI plugin to implement a Custom Post Type. Then, the most important part is how to add Custom Fields in our Custom Post Type. We used the Advanced Custom Fields plugin (ACF) to get the result.
Your feedback about this article will be valued at WP inCare. Do add your suggestion in our comments section for providing you better WordPress content 🙂