Create Custom Post Types in WordPress the Proper Way

Create Custom Post Types in WordPress

Before jumping on how to create custom post types in WordPress, let’s first discuss what is custom post types in WordPress and why we need to create custom post types in WordPress.

What is Custom Post Type?

WordPress categorizes different types of content in the form of Post Types. WordPress comes with built-in Post Types that are as follows.

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

A custom post type is a feature that allows you to define and create custom post types in WordPress beyond the default post types that come with WordPress. This enables you to organize and display various types of content in a structured and user-friendly manner.

Why create Custom Post Types in WordPress?

WordPress, by default, is perfect for blog sites where you can post articles and upload images. Custom post types are a powerful way to extend WordPress’s functionality, enabling you to organize various types of content. For example, if you want to have your client testimonials, a better approach would be to create a custom post type ‘Testimonials’ and save the data. This will make your testimonial content more organized and managed.

Similarly, you can create custom post types in WordPress such as portfolios, products, and others according to your requirements. You can also have separate templates for each custom post type’s archive and single page. As a result, you may customize the layout to meet your needs and improve the user experience.

How to Create Custom Post Types in WordPress?

You can create custom post types in WordPress using different plugins like Custom Post Type UI. However, in this article, we won’t be using any plugins. Instead, we will create the custom post types manually. Let’s now create a custom post type ‘testimonials’. To create custom post types in WordPress we use an inbuilt PHP function register_post_type.

Follow the steps to create custom post types in WordPress

Go to the functions.php file of the active theme from the theme directory. Please make sure you work on the child theme if you are using any prebuilt themes. By doing that, theme updates will not affect your codes.

Please add the following code in functions.php

/**
 * Custom post type function testimonials
 **/
function create_custom_post_type() {
    register_post_type( 'testimonials',
        array(
            'labels' => array(
                'name' => __( 'Testimonials', 'your-text-domain' ),
                'singular_name' => __( 'Testimonial' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'testimonials'),
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_custom_post_type' );

You should see the menu item ‘Testimonials’ appear in the admin area.

Custom Post Type in the WordPress Backend

If you want more options for your custom post type, you can use the following code instead of the one above.

/*
* Custom post type function testimonials
*/

function create_custom_post_type() {

	// Set labels for Custom Post Type
	$labels = array(
		'name'               => __( 'Testimonials', 'your-text-domain' ),
		'singular_name'      => __( 'Testimonial', 'your-text-domain' ),
		'menu_name'          => __( 'Testimonials', 'your-text-domain' ),
		'parent_item_colon'  => __( 'Parent Testimonial', 'your-text-domain' ),
		'all_items'          => __( 'All Testimonials', 'your-text-domain' ),
		'view_item'          => __( 'View Testimonial', 'your-text-domain' ),
		'add_new_item'       => __( 'Add New Testimonial', 'your-text-domain' ),
		'add_new'            => __( 'Add New', 'your-text-domain' ),
		'edit_item'          => __( 'Edit Testimonial', 'your-text-domain' ),
		'update_item'        => __( 'Update Testimonial', 'your-text-domain' ),
		'search_items'       => __( 'Search Testimonial', 'your-text-domain' ),
		'not_found'          => __( 'Not Found', 'your-text-domain' ),
		'not_found_in_trash' => __( 'Not found in Trash', 'your-text-domain' ),
	);

	// Set other options for Custom Post Type Testimonials.
	$args = array(
		'label'               => __( 'Testimonials', 'your-text-domain' ),
		'description'         => __( 'Clients reviews and feedback.', 'your-text-domain' ),
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields' ),
		'taxonomies'          => array(),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'post',
                'rewrite'             => array('slug' => 'testimonials'),
		'show_in_rest'        => false,
	);

	register_post_type( 'testimonials', $args );
}
add_action( 'init', 'create_custom_post_type' );

In the above code, the variable $labels is responsible for defining the labels visible in the admin area. Additionally, the ‘hierarchical‘ argument is set to determine the custom post type’s behavior: when set to true, it acts like a page; when false, it functions like a post. You can read more about the difference between posts and pages here.

Similarly, you can see the ‘show_in_rest‘ argument and set it to false. If you set it to true, it will create a route within the wp/v2 namespace. Also, setting it to true will enable the Gutenberg Block Editor.

Similarly, the ‘supports‘ argument specifies the features supported by the custom post type. In the code above we have included the title, excerpt, editor, author, thumbnail, comments, and custom meta. You can read more about options from here.

Create a Post in the custom post types in WordPress
Custom Fields and Comments settings of a Post in WordPress

Finally, go to Settings > Permalinks and click on the Save Changes button. This will make the necessary .htaccess changes.

Conclusion

That concludes the process. You have successfully created a custom post type ‘Testimonials.’ We trust that this article has provided you with the knowledge needed to create custom post types within WordPress.

FAQs About How to Create Custom Post Types in WordPress

What is the difference between post and custom post type in WordPress?

Post and page are the default post types in WordPress, with built-in categories and tags. Custom Post Types, on the other hand, are content types you create yourself, offering flexibility for various content needs, such as products or events. You get to design how they function and appear, including their categories and tags. Posts are used for conventional blog material, however, custom post types allow you to create your own content types for varied purposes on your WordPress site.

How do I create custom post types in WordPress using plugins?

You can create custom post types in WordPress using Custom Post Type UI. You can see our article on how to install plugins in WordPress.