How to Create Custom Post Type in WordPress

Create Custom Post TypesIf you want to create a custom post type in WordPress without a plugin is a straightforward and simple process that does not require you to be a WordPress developer or an experienced WordPress user.

First, before we can create a custom post type without using a plugin, it helps to explain the basics of how posts work in WordPress but if you understand the basics; you can skip to this introduction section of creating custom post types.

What are Custom Post Types?

WordPress is the fastest-growing content management system but it was initially designed as a blogging CMS. It has over the years evolved to be a content management system that is used to build a robust website across all industries.

At the core of WordPress’s evolution from a blogging CMS to a full resource CMS is the custom post types.

When WordPress was designed the initial idea was to have posts and pages that could be used to publish blog posts and pages to publish static content that would not change often.

Ideally, WordPress was solving the problem of static websites by introducing a new way to easily and quickly publish new content and keep publishing more and more content without editing a single line of code.

To make WordPress more useful beyond blog publishing Custom Post Types were introduced with the aim of allowing users to create their own types of posts that could be anything they can imagine.

This custom post type allows users in diverse industries to add content to their websites that relate to their business.

Custom Post Type Example

Here is a specific example: a hotel owner can use WordPress to build the hotel website and leverage custom post types to showcase the different rooms in the hotel or the different services related to their business; standard room, single room, etc.

With this approach content published can be organized in the right way and managing the content becomes very easy.

Why Create Custom Post Types?

The overall reasons why we create custom post types are to extend the capabilities of WordPress to fit different needs.

Example: As a WordPress developer, artist, author, or creative professional, when creating a portfolio website; you cannot use the existing WordPress post and pages to showcase your work.

You can instead create a custom post type for the portfolio and use that kind of post to showcase your work and you will have better control and the appearance on your portfolio website.

Custom post types exist so that they can extend the core functionality of the website where ordinary posts and pages are limited. So now how do we create the custom post types?

How to Create Custom Post Types?

There are two major ways of creating custom post types in WordPress:

  • Creating custom post types with a plugin.
  • Creating custom post types without a plugin.

In this article, I will focus on the second one on how to create a custom post type in WordPress without a plugin.

First, you need to understand the basics of how to add code to your existing WordPress theme.

Creating a Custom Post Type in WordPress without Plugin

Step 1: Name of Custom Post Type

Decide the name of your custom post type. A basic example, of the custom post type for the hotel, can be rooms where we can publish the different types of rooms in that hotel with their amenities and the prices.

So we will add the name of the custom post type when we are registering it as “room”

Step 2: Register Custom Post Type

The second step is to register the custom post type in your WordPress theme.

You should add the following code to your function.php file:

//Room Custom Post Type Example

add_action('init', "room_custom_post_type_example");

function room_custom_post_type_example(){

   $labels = array(
              'name' => _x('Rooms', 'post type general name'),
              'singular_name' => _x('Room', 'post type singular name'),
              'menu_name' => _x('Rooms', 'admin menu'),
              'name_admin_bar' => _x('Room', 'add new on admin bar'),
              'add_new' => _x('Add New', ''),
              'add_new_item' => __('Add New Room'),
              'edit_item' => __('Edit Room'),
              'new_item' => __('New Room'),
              'all_items' => __('All Room'),
              'view_item' => __('View Room'),
              'search_items' => __('Search Rooms'),
              'not_found' => __('No Rooms found'),
              'not_found_in_trash' => __('No Rooms found in Trash'),
              'parent_item_colon' => __('Parent Roms:'),

         );

$args = array(
               'hierarchical' => true,
               'labels' => $labels,
               'public' => true,
               'publicly_queryable' => true,
               'description' => __('Description.'),
               'show_ui' => true,
               'show_in_menu' => true,
               'show_in_nav_menus' => true,
               'query_var' => true,
               'rewrite' => true,
               'query_var' => true,
               'rewrite' => array('slug' => 'room'),
               'capability_type' => 'page',
               'has_archive' => true,
               'menu_position' => 22,
               "show_in_rest" => true,
               'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', 'page-attributes', 'custom-fields' )
      );

               register_post_type('room', $args);
}


This code adds the custom post type to your website and it appears in your WordPress dashboard and you can now start publishing the content on your new custom post type.

how to create custom post type in wordpress without plugin

You can learn more about registering custom post types.

Step 3: Publish Content on Custom Post Type

how to create custom post type in wordpress without plugin

Publish your custom post type and before you can visit the front end make sure you flush the permalinks to avoid getting a 404 page not found when you check the front end.

Conclusion

With these three steps, you can easily create a custom post type in WordPress without using a plugin and begin publishing your custom content with custom templates that are specific to your custom post type.

If you are not sure about how to make this work for your website, I would like to recommend further reading on registering custom post types

Similar Articles

  1. How to Get Order ID In Woocommerce

Comments are closed.