One of the powerful features of WordPress include custom post types. They give you the crucial flexibility you need to develop a custom website and transform WordPress from a basic blogging tool to a content management system.
But they can be confusing: what exactly are WordPress custom post types, how do they differ from regular post types and how do you create them, how do you add to them, how do you display them on your site? This article will guide you about custom post types and how to create them in WordPress.
What is Custom Post Type in WordPress
Post types in WordPress help in identifying various WordPress content types. Although pages and posts are both post types, they serve different purposes.
During WordPress installation, the following post types come by default:
- Post: Typically used for the blog post
- Page: A simple static page
- Attachment: To attach media files
- Revision: To view the post revision
- Navigation Menu: To view the menu
Apart from the default post types, WordPress comes with a feature where you can create your own custom post types. These are typically useful when your website demands a different format than that of a default post and page.
What is the Difference between Default Post Type and Custom Post Type?
A “post” is a common content type in WordPress that is utilized for blog posts and news articles. It can be organized by categories and tags and has specific fields like the title, content, and author.
A custom post type, on the other hand, is a kind of content that you design yourself using custom fields and a custom layout. It allows you to create content with different structures and fields than the default posts like a portfolio or events listing.
Custom post types can be made using WordPress plugins or bespoke development, and they have their own menu item in the WordPress admin dashboard. Similar to the archive for regular posts, they have their own page and can be searched for and displayed in various ways on your website.
In general, custom post types offer flexibility in creating different types of content with their own distinct fields and structure, even though posts are the default content type in WordPress.
Example of Custom Post Type
To explain with an example, we will use the WooCommerce plugin. WooCommerces Creates Products and Orders Custom Post Type to handle product and order Data.
How to Create Custom Post Type Manually
Creating this type has two ways: manually and by using a plugin. Let us first get started with creating a custom post type manually. Follow the steps below:
- Go to Appearance> Theme File Editor> function.php. All the changes that you have to make to create a custom post type are to be made in the function.php file.
- Use the following code:
3. In the above code, update the Prefix, Post type name, and labels in prefix_cptname, Labels, and cpt_slug according to your need.
4. Paste the updated code in the function.php file and click on Update File.
5. After the file is updated, you can now see that the new post type pops up in the admin panel of your dashboard.
To create a new post of this post type, simply click on Add New and fill in the details.
6. If you do not want to go through the tedious process of writing the code, a great alternative is to go to GenerateWP. This would assist you in writing your code without committing any errors and give you a precise vision of where to make the changes.
Fill in the details for all tabs: General, Post Type, Labels, and so on. Click on Update Code and copy the updated code to paste it into the function.php file.
Now shift your focus to creating a custom post type using a plugin.
How to Create Custom Post Type with Plugin
You can use a plugin that gives you an interface made to make registering a custom post type simple if you prefer to do so over using one that is a part of the larger functionality offered by a plugin like WooCommerce.
Custom Post Type UI is one of these plugins.
Customizing the Post Type
All themes in WordPress follow the Template Hierarchy. If you want to customize the display of a newly created post type, then you can create new files for CPT in the theme that will overwrite the display mechanism.
Extending Custom Post Type
CPTs are nothing but normal posts with some extended information. Extended information is stored in post meta, commonly known as Custom Fields in WordPress Dashboard. Custom fields can be visualized in various meta boxes.
We hope this article has been of great assistance to you in learning about Custom Post Types and how to create them. If you have any queries regarding custom post types, do let us know in the comments.