How to create WordPress CPT-custom post types

What are custom post types?

WordPress has been the most popular content management system for years. If you are familiar with WordPress, I am sure you must have worked with posts and pages. These are the most popular post types that we use in WordPress. But, do you know that posts and pages are only two types of post types. WordPress provides more than two types of post types for content management. The following is a list of Default Post types in WordPress.

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Navigation Menus
  • Custom CSS
  • Changeset


But other than that you can create your post type as well. WordPress calls these custom post types.

How to create it programmatically

In order to create custom post types, you need a register_post_type WordPress function.
This function takes two arguments
1. $post_type
This is a mandatory parameter. This is the identifier of the custom post types that you are going to create. It should ideally be in lowercase letters. It can also contain alphanumeric characters, dashes, and underscores.
2. args
This is an optional parameter. It can have arrays or/and strings as arguments. WordPress documentation gives a list of all the argumentation that you can use for args.

function book_posttype() {
 
    register_post_type( 'books',
    // CPT Options
        array(
            'label' =>"Books",
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'Books'),
            'show_in_rest' => false,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'book_posttype' );

You can add the above code to the functions.php file of your theme. I recommend first you create a child theme, have a functions.php file in the root folder of the child theme, and then add the above code.

let’s take a look at the code above.

‘books’ is the identifier for the custom post type that we are going to create. This is the required parameter.

Args is the second argument of the register_post_type. it can have arrays and/or String as the parameters. Args is an optional argument.

‘public’ is set to true, this makes sure the post type is for the use public on WordPress dashboard or at the front-end.

‘rewrite’ has an array and ‘slug’ as the key. This will change the permalink slug for an instance of the custom post type. But, this depends on your permalink settings. Your Permalink settings should have %postname% in the permalink structure.
For example, if you add a new book name with the title “The Great Gatsby”, the default permalink would be
https://www.example.coma/Books/the-great-gatsby/


setting ‘show_in_rest’ to true will enable you to add or edit custom post type in the block editor. Default is ‘false’. So, if you do not set this key, WordPress classical editor loads for adding or editing custom post types.


Once you add the above code to your functions.php file, the custom post type should be visible as below

book cpt

How to create CPT with plugins

Another way to add custom posts is to use plugins. Custom post UI is a popular UI to manage custom post types.
Install Custom Post Type UI and it show appear as CPT UI in the admin panel.


There is an “Additional labels” panel that you can use to add more customization for the custom post types. This includes Post Type Description, Menu Name, All Items, search item, and so on. Each label has a short description that is easy to understand.


The settings panel has many true/false values to set up. For the most part, you do not need to change these settings for basic usage of custom post types. You need, at least, intermediate knowledge of WordPress to change some of these settings.


In general, the plugin is a good choice if you are unfamiliar with WordPress programming. It can also be a better choice because plugins updates as WordPress updates its versions.

Conclusion

If you find that the default post types that WordPress offers are not enough for your project, you can create your own post types to manage the content of your website. You can do this with WordPress functions and using plugins. Plugins are a good choice for non-technical users as well as for developers. please feel free to add any comments for this post as It helps me to improve the quality of my posts.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top