How to Create a Custom Post Type in WordPress Without a Plugin

Updated: February 1, 2021

WordPress is an open source content management system (CMS). No wonder if it has a very open ecosystem. Being an open source software, WordPress is fully customizable. Not just in terms of appearance but also in terms of functionality. You can add new functionality to your WordPress site, be it via plugins or manually. One of the ways to add new functionality to your WordPress site by creating a new custom post type (CPT).

What is a custom post type?

In WordPress, the term “post type” refers to the content type. Be it blog posts or pages. Referring to this documentation page, there are five default post types in WordPress:

  • Post (blog post)
  • Page
  • Attachment
  • Revision
  • Navigation Menu

If you want to create a certain website type (such as an IMDb-like movie database site), you might want to create a new post type for an individual movie page where you can add more information like rating, genre, release date, and so on — in separate fields. This is where a custom post type comes into play. You can create a custom post type to cover the custom content type according to your needs. While for the additional information (rating, genre, release date, and so on), you can create custom fields.

There are two options to create a custom post type: manually and via a plugin. This article will show you how create a custom post type in WordPress via the first method.

How to create a custom post type in WordPress manually

To create a custom post type without a plugin — manually — you need to add a new function to register the custom post type. You can do so by editing the functions.php file of your theme. First, log in to your WordPress dashboard and go to Appearance -> Theme Editor. Select the functions.php file on the right panel to edit it. Before start editing the file, we suggest you back up the file first just in case everything doesn’t go as you expected.

Add the following code right after the last line of functions.php file content. Click the Update File button to apply the changes.

The code:

// REGISTER CUSTOM POST TYPES
// You can register more, just duplicate the register_post_type code inside of the function and change the values. You are set!
if ( ! function_exists( 'create_post_type' ) ) :

function create_post_type() {
	
	// You'll want to replace the values below with your own.
	register_post_type( 'movie', // change the name
		array(
			'labels' => array(
				'name' => __( 'Movies' ), // change the name
				'singular_name' => __( 'Movie' ), // change the name
			),
			'public' => true,
			'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ), // do you need all of these options?
			'taxonomies' => array( 'category', 'post_tag' ), // do you need categories and tags?
			'hierarchical' => true,
			'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
			'rewrite' => array ( 'slug' => __( 'movies' ) ) // change the name
		)
	);

}
add_action( 'init', 'create_post_type' );

endif; // ####

Four lines you need to notice are:

  • register_post_type( ‘movie’: This line is used to register a new function called “movie”.
  • ‘name’ => __( ‘Movies’ ): This line is used to define the plural label on the WordPress dashboard.
  • ‘singular_name’ => __( ‘Movie’ ),: This line is used to define the singular label on the WordPress dashboard.
  • ‘rewrite’ => array ( ‘slug’ => __( ‘movies’ ) ): The “movie” text on this line will be used on the URL structure of your website.

You can replace the texts above according to your needs.

How to create a custom template for your new custom post type

On the code above, the content of your custom post type will be displayed using the default template of the default post type (blog post) on your theme. If you want to display the content of the custom post type using a different layout, you can create a custom template. To ease your job, you can use a plugin like Divi Builder, Elementor, or Brizy, which has a theme builder feature to create a custom template for WordPress post types. In this post, we will show you how to create a custom template for the custom post type using Divi Builder.

Creating a custom template using Divi Builder

Assuming you have installed and activated Divi Builder on your WordPress site, go to Divi -> Theme Builder on your WordPress dashboard. Add a new template by clicking the plus button. On the appearing dialog, select All Movies and click Create Template.

Once the template is added, click Add Custom Body -> Build Custom Body to start creating the template. Clicking the Build Custom Body button will take you to the Divi Builder editor.

On the next step, select the leftmost option and click the Start Building button.

On the next step, you will be asked to add your first row before you can add modules. Simply insert one. For more detailed instructions on how to create a custom template for a single post using Divi Builder, you can refer to this page.

Once you are done creating the template, you can click the Save button on the bottom-right corner, followed by the X icon on the top-right corner to exit the Divi Builder editor.

You will be taken back to the Theme Builder page after clicking the X icon above. Click the Save Changes button to apply the custom template you have just created.

The bottom line

By default, you can only create two content types on your WordPress site: blog post and page. Cust post type (CPT) is a customization feature of WordPress you can make use of to allow you to create other content types which you can define yourself according to your needs. If you don’t want to use a plugin to create a custom post type, you can add a new function to register a custom post type as we have just covered above.

This page may contain affiliate links, which help support utilizeWP.

Boost your site speed with the best caching plugin in the world

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This