How to Create WordPress Custom Post Type – The Ultimate Guide

 

This is an excellent and useful way to organize your website if you are running multiple types of content simultaneously. In this tutorial, you will learn how to create a WordPress custom post type by building a site-specific plugin.

What is a WordPress custom post type? 

WordPress provides some default post types, such as post, page, revision, and attachment. On a smaller scale, they will accurately present your blog topic. However, they may not be enough once your content is more diverse.

Where WordPress custom post type comes into play. In short, this is an additional post type that you create based on your needs. With this, you will be able to group your posts specifically.

Creating custom post types in WordPress is more efficient than assigning categories to your posts. Because categorizing articles puts them in the same list, usually in the post section. The problem is, if you have more than one broad subject, it will be difficult for you to monitor them.

On the other hand, it lets you choose a more appropriate section for your post from the beginning. This should help you classify your content better and more basically.

The WordPress custom post type has its own menu in the WordPress admin area, which leads to a list of posts of that particular type.

New WordPress Custom Post Type

In the example above, if a user visits the Custom Articles Database section of your site, the posts, or posts from the page, will not be included. This is because they are of their type.

Not only that, you can help reduce your searches by adding groups to the right post, such a great feature for your visitors in various categories of custom articles, such as reviews, tutorials, etc.?

What can you change to a WordPress custom post type? 
You can choose where the menu appears in the admin area, whether that type of post is included in the search results, whether it supports excerpts, whether comments are allowed, and so on.

In addition, the WordPress custom post type allows you to enable the custom fields feature in the post editor. Thanks to this, it is possible to provide more information about your content.

Take a look at our custom article post example. In addition to keeping the article as its main content, you have the option to add custom fields for information such as author, reading time, etc.

Custom field form under your post editor

However, any custom fields that you create are available on all post types. Therefore, a plugin is required to display a specific field only for a certain post type. We will explain how to do this later, so keep reading.

To start a WordPress custom post type element 

learn to write a new function that calls the register_post_type () function. It should come with two parameters:

Post type name that should not be more than 20 characters long and should not contain spaces or capital letters. Our custom post type name is article. 
An associative array named $ args that contains information about the post type in the ‘key’ = ‘value’ pairs.

Finally, all custom functions must be prefixed to avoid collisions with other plugins or theme functions. To make them specific, we are using the Hostinger Tutorials Initial for this example – ht.

To add a new custom post type, the initialization code should look like this:

Since $ args and $ label are both arrays, it is better to write the $ label variable first and then the $ args variable. Once this is done, you can register the code.

Now, we are going to discuss $ args and $ label arrays, which we will use to customize the WordPress custom post type.

What is $ args? 

$ args is short for arguments and this variable is used to include arrays. On the other hand, an array is a data structure that stores elements (key and value pairs) of an object (WordPress custom post type).

There are many arrays in $ args, but we are only moving to the most used list. However, keep in mind that they are all optional.

Label – An array that defines various pieces of text, such as the menu name and the WordPress custom post name itself. We will talk about this array later in the $ label array section. 
It can be shown in a post type template and will not be used anywhere else. 
Public – To change the visibility of WordPress custom post types for authors and visitors. We are going to set the value of TRUE to display it in the WordPress dashboard.

LEAVE A REPLY

Please enter your comment!
Please enter your name here