How to Create Custom Post Types in WordPress?

Table of Contents

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:

  1. Post: Typically used for the blog post
  2. Page: A simple static page
  3. Attachment: To attach media files
  4. Revision: To view the post revision
  5. 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. 

custom post

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:

  1. 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. 
  2. Use the following code:

custom post

 

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.

custom post

5. After the file is updated, you can now see that the new post type pops up in the admin panel of your dashboard. 

custom post

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. 

custom post

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. 

custom post

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.

custom post

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. 

custom post

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. 

custom post

The above image shows the product data for the WooCommerce plugin, product cpt. To create a custom meta box, you can either code or use a readily available plugin/ metabox or CMB2

Conclusion

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.

Fully Managed WordPress Hosting

Nestify’s AWS powered dedicated CPU servers keep your sites fast, secure, and always up to date.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.