Why and How to use Custom Post Types in WordPress?

In WordPress everything is Post. When we add a new Page, that resides in the database as post. Then why do we need custom post types in WordPress? Moreover, if we really need, then how could we achieve this?

This post will discuss the above topic. However, we should think of the beginners who don’t have prior knowledge of PHP. So, we need to be as simple as possible.

Firstly, let us discuss why we need custom post types. Especially, when we can manage everything from either Post, or Page.

Why do we need it?

The purpose should be clear. Whatever we do, we should know why we are going to do this?

Secondly, when we understand why we need custom post types, it would be easy for us to be creative.

Hope, that makes sense.

So, let us start with the first question. But before that let’s know what is custom post types?

What is custom post type in WordPress?

Custom post type is also a kind of post. That means we’re not doing nothing new or extra. In short, WordPress comes with its own post types that we already know.

Post and Page.

However, if we want to make WordPress more than a simple blog platform. That is to say, if we want to uplift the power of WordPress, then there is no other way around except than custom post type.

Why so?

Consider a situation where we want to keep our published books in a separate hierarchy. Of course we could have done that either with post or page.

For example, we could have made a Book category. And just point our default post type to them.

But that’d not serve the purpose of creating generic content. A different content type that does not belong the hierarchy of either Post, or Page.

For this special content you can create different type of tags, different type of categories, even a field where you give some excerpts of the post.

For instance, we can see an image of custom post type with extra features:

A custom post type with extra features
A custom post type with extra features

Subsequently, WordPress treat these types of categories, tags and excerpts as custom Taxonomy. Search engines index them separately.

WordPress makes those things possible in two ways.

We’ll see that in a minute.

Keep reading.

Why we need custom post types in WordPress?

Now, we know why we need custom post types in WordPress.

Suppose we need a special type of posts that lets users know what kind of Books you’ve published. Firstly, we treat it as a page with relevant image and link.

Now, each link takes us to the detail page.

With the help of custom post type, we can even change the design of the page by using Gutenberg editor. As a result, we can add tables, rows and columns.

These blocks will change the look of custom post types.

Possibility is endless.

On the contrary, in default Post or Page type, we cannot change this.

We have to choose between any two editors.

Either Classic, or Gutenberg.

However, while writing a custom post type, we can control that with a single boolean value – true.

Custom post type adds a Gutenberg editor on the fly
Custom post type adds a Gutenberg editor on the fly

Where are custom post types stored in WordPress?

WordPress stores all types of posts in the same place.

The wp_post database table keeps custom post types along with the other posts. However, a separate column post_type differentiates it with other columns in the same table.

With the help of custom post types we can extend the typical boundary of WordPress.

On the other hand, default post or page do not support that.

We can call it an extension. The custom post types.

It extends beyond known taxonomies. Category and tag.

How do I create a custom post type in WordPress

If we want to use custom post type, we have to create it. Okay?

How can we create a custom post type?

There are two ways.

We can either create it in the functions.php file. For example consider this:

// future post type
function custom_future_post_types() {
    register_post_type( 'future', array(
        'public' => true,
        'labels' => array(
            'name' => 'Future Posts',           

        ),
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'taxonomies'         => array( 'category', 'post_tag' ),
        'show_in_rest'       => false,
    ));
}

add_action('init', 'custom_future_post_types');

Suppose we want to let users know what type of Future posts are coming in the next week. As a result, every Monday, we can write a new custom Future post type.

Should I use custom post types?

Of course, you should.

There are a lot of reasons to do so.

The first and foremost reason is variety. We can play with our custom post types. Meanwhile, default post or page doesn’t allow us to do so.

Most importantly, when we have deeply nested pages, custom post types come to our help.

Why?

Because we can create separate permalink for them. That we cannot do with the default child pages.

As we’ve said, the possibility is limitless.

However, there is one caveat.

Every functions.php file resides in our current theme folder. Right?

What will happen to our custom post types, if we want to change the theme?

Well, WordPress has thought about it. Don’t worry.

In the WordPress document root, along with the plugins we can create another folder and name it ‘mu-plugin’. It stands for must use plugins.

Just like creating a plug in, we can add any PHP file to that folder and keep the above function there.

Now we are safe to use it as it shows exactly the same way on our dashboard.

What Next?

Books at Leanpub
Books in Apress
My books at Amazon
GitHub repository
Technical blog
Twitter

Comments

2 responses to “Why and How to use Custom Post Types in WordPress?”

  1. […] Similarly, in my previous post, I had discussed why we use custom post type. […]

  2. […] Similarly, in my previous post, I had discussed why we use custom post type. […]

Leave a Reply