Custom Post Types: How to create your own post types?


WordPress has always been presenting endless possibilities and unabated flexibility to customize its architecture as per your requirements. Custom post types happen to be the great representation for this flexibility.

I’m going to throw more light on the custom post types, a feature often implemented by developers to achieve specific data types to be displayed over a website. Generally they are quite similar to the standard posts that come as defaults with any WordPress site and have almost exactly same structure. For folks who are still midway in their understanding of custom post types, here is a simple enough example:

Let’s say you have a mobile phones sales site and you need a method by which you can keep posting the phones you have for sale and also the ability to update the page with different phones on a weekly basis. For this type of content that needs to be updated very frequently, posts and pages do not suffice. With custom post types, you can create a custom post type called “Phones” that allows you to include specific content type for phones, without mixing it up with posts and pages. You can play around with informational bits like price, discounts, software, etc.

So, a custom post type helps you when:

- Your website requires a blog section with different categories, widgets etc.
- Website requires a content structure in which admin will post specific information about, lets say, events, images and other information.

Now to make the admin panel more user friendly, developer can create a custom post type named as “Manage Events” and allow the admin to get in there and enter content and corresponding information accordingly. It will be simple and clear to understand whenever a non-tech-savvy admin checks the admin panel, besides, it will be easier to segregate the content.

Now, coming to the technical aspects of the Custom Post Types, I am including below some sample codes and process to elaborate how you can easily implement custom post types in your WordPress set up.

1- First step will be to register the information with CMS, and to do so, you need to include the particular information in system so that WordPress can understand it’s information for Custom Post Type:

function my_post_type(){
$args = array(
'label' => 'My Custom Post Type',
'show_ui' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'post-slug'),
'menu_icon' => 'dashicons-video-alt',
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes')
);
register_post_type('custom-post', $args);
}
add_action('init', 'my_post_type');

This piece of code must be included within the function.php file so that as soon as WordPress accesses the files, the information gets registered with it. Now, let me explain its different code chunks below:

function my_post_type(): Essential line used to initiate process of creating custom post type.

label : Name of the post type, if we don’t define a label, it will use “custom post type” by default.

show_ui : Generate a default user interface of custom post types, that means it will look exactly like other elements of WordPress admin, especially the default blog post management option.

hierarchical: It defines the hierarchy of the posts of CPT, like whether we want to declare parent or child posts, or more like posts or sub posts.

rewrite: This argument is to set the base slug of the custom post type, which is to say what will the URL be once we open the root of custom post type in browser. In this case it will be www.xyz.com/post-slug/

menu_icon: “menu_icon” is used for managing the post type icon in admin area. Reference link for menu icon http://melchoyce.github.io/dashicons/

supports: It defines the features which will be supported by the custom post type being created here.

register_post_type(‘custom-post’, $args): After initiating with function, this function marks that all the arguments are supplied and will have properties as provided in arguments.

add_action(‘init’, ‘my_post_type’): “add_action” hooks this function on to WordPress for data flow.

Now, once you are done with this, you will have your custom post type ready in backend, and you are all set to start using it anywhere in your set up. Your menu of admin panel will include the custom post type and will look somewhat like the following image.