Dinesh Imaduwa Gamage
Daminda Dinesh

Daminda Dinesh

How To Create Wordpress CPT-custom Post Types

How To Create Wordpress CPT-custom Post Types

Dinesh Imaduwa Gamage's photo
Dinesh Imaduwa Gamage
·Apr 6, 2022·

4 min read

Table of contents

  • What are custom post types?
  • How to create it programmatically
  • How to create CPT with plugins
  • Conclusion

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' );

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 the 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

example.coma/Books/the-great-gatsby

setting ‘show_in_rest’ to true will enable you to add or edit custom post types 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

image[1].png

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 shows appears as CPT UI in the admin panel.

image-1[1].png

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 update 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.

 
Share this