How to create a custom post type in WordPress?

Hello Learners! With the new beginning of 2020, let’s start something new related to WordPress. So, be ready to learn how to create a custom post type in WordPress within simple six steps.

A Post type refers to the various structured data that is maintained in the WordPress posts table. These can be posts, pages, revision. attachment (media), navigation menu items.

Besides these, if you want to add a new post type you’ll have to create a custom post type in WordPress. The function that will help you to create a custom post type is ‘register post type’.

register_post_type() should only be invoked through the ‘init’ action.

‘Register post type’ is a WordPress built-in function that creates or modifies a post type. Syntax: register_post_type(). Additionally, this function should only be invoked through the ‘init’ action. Calling register_post_type before init or after init both will create a problem. So note this point to invoke the function successfully.

Register post type takes two parameters. i.e. register_post_type($post_type, $args). Where $post_type is required and $args is optional. $post_type is a string that stores the name of the post type. On the other hand $args is a set of arguments such as label, labels, description, public, exclude_from_search , show_ui, show_in_menu, menu_position, menu_icon, show_in_nav_menus, and more. You can see more on function reference .

Now let’s make a WordPress plugin that creates a custom post type called ‘Todo’. You don’t know how to create a WordPress plugin? Don’t worry because here we’ll start from the root. First of all, we’ll create a plugin, activate it and then create a custom post type that appears in the admin menu.

Let’s Start

Step 1:

Open the directory where your current site lies. Go to wp-content -> plugins and create a new folder named ‘todoApp’. You can name the plugin whatever you like. However, it is good practice to give the name after what the plugin does to your site.

Step 2:

Now open the folder in any text editor, here we use PhpStorm (10.0). And create a PHP file under the plugin’s folder. Like, we create an index.php file under the todoApp folder.

Create index.php file.

Step 3:

Before starting to code, let’s set the environment for WordPress plugin development first. If you are using PHPStrom, go to setting -> language and framework -> WordPress and check the checkbox saying ‘Enable WordPress Integration ’. Click on ‘Apply’ and ‘Ok’. Then locate the WordPress installation path. This will help you through your plugin development by providing WordPress specific coding assistance as well.

Setting up the WordPress coding environment.

Step 4:

Now its time to create header comment which is mandatory for a WordPress plugin. The syntax for header comment is slightly different from general comments. Inside the <?php ?> tag, use /** to open the header comment tag, each lines followed by * and lastly */ to close it. In the header comment, we need to provide some meaningful information about the plugin. Like the name of the plugin, description, version number, Author and some more where name is compulsory and others are optional.

Add header comment

Step 5:

As we discussed earlier, we will be using register_post_type function here. Just create a php function naming “my_custom_post_type()”. Inside ‘my_custom_post_type()’ define the function ‘register_post_type’ with its parameters $post_type and $args.

function my_custom_post_type()
register_post_type('Todos', $args= array(
'label' => 'My Todo List',
'public' => true,
'menu_icon' => 'dashicons-editor-ul',
'show_ui' => true,
'show_in_nav_menus' => true,
'name' => 'Todo List',
'description' => 'This is our custom post type'

According to the above function, you are creating a custom post type, ‘Todos’. The post will be labeled as “My Todo List ” and will be displayed in nav_menus.

Step 6:

After defining the function, we are now ready to invoke the function through the ‘init’ action.

add_action('init', 'my_custom_post_type');
custom post type function

This will successfully create a custom post type named “Todos” for your plugin. And as for general posts, WordPress automatically handles CRUD operation for the custom post type as well.

Custom post type created.
Showing in nav_menu.

Alright, that was all for today. I hope this tutorial was helpful to you. Now you can download this GitHub repository of the tutorial. Thank you for the time! Eagerly waiting for your feedback. :)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store