How to Create Custom Post Types in WordPress

WordPress

We all know that WordPress is the best blogging platform but it is not just limited to being a blogging platform. It allows us to create any kind of website without even knowing the coding. There are so many WordPress themes that take benefit from the custom post type feature of WordPress and allow us to have any kind of website. So, the custom post type is an important thing to know if you are starting with WordPress. In this article, I will explain what is custom post type and how you can create a custom post type in WordPress.

We Recommend Bluehost

Best Managed WordPress Hosting

WPEngine is the best and most secure managed hosting provider

What is Custom Post Type in WordPress?

In WordPress, we have different kinds of default post types. These default post types are:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

But, you can also create your own post type to manage a different category of post in a separate way. If you create a new post type on WordPress other than using the default one, this is called Custom Post Type.

For example, if you want to create an event website, you will think of having a different post type for event booking. On Techlomedia, the theme has a different post type for reviews. There could be more like coupons, testimonials, or portfolios.

How to Create Custom Post Types

Creating a custom post type on WordPress is not difficult. You can either do it by using a WordPress plugin or you can add code in the functions.php file. I will explain both ways here.

Easy Way: Create a custom post type with CPT UI

Custom Post Type UI or CPT UI plugin is the easiest way of creating a custom post type in WordPress. Not just post types, you can also add new taxonomies with the help of this plugin.

Note: The custom post type will remain on your blog until you have this plugin. If you uninstall the plugin, your custom post type will stop working.

Install and activate this plugin. It will add a new menu item ‘CPT UI’ in your WordPress admin menu. Navigate to CPT UI -> Add New to create a new custom post type in your WordPress website.

Create a custom post type with CPT UI

Enter the Post Type Slug and labels to add a new post type.

The next section on the page asks for additional information. You can provide those additional labels as per your interest.

Creating custom post type manually

With the plugin, you do not need to worry about complexities and you can easily create a custom post type. If you think to do the same manually, you will have to put in lots of effort.

The most basic code for creating a custom post type is this.

function coupon_posttype() {
	register_post_type( 'coupons',
		array(
			'labels' => array(
				'name' => __( 'Coupons' ),
				'singular_name' => __( 'Coupon' )
			),
			'public' => true,
			'has_archive' => true,
			'rewrite' => array('slug' => 'coupons'),
		)
	);
}
add_action( 'init', 'coupon_posttype' );

This code will register a post type ‘coupon’ with an array of arguments. But there can be a detailed piece of code with more options for custom post types.

function coupon_posttype() {
	register_post_type( 'coupons',
		array(
			'labels' => array(
				'name' => __( 'Coupons' ),
				'singular_name' => __( 'Coupon' )
			),
			'public' => true,
			'has_archive' => true,
                        'show_ui' => true,
                        'show_in_menu'        => true,
                        'show_in_nav_menus'   => true,
                        'show_in_admin_bar'   => true,
			'rewrite' => array('slug' => 'coupons'),
                        'supports' => array(
                                        'title',
                                        'editor',
                                        'excerpt',
                                        'trackbacks',
                                        'custom-fields',
                                        'comments',
                                        'revisions',
                                        'thumbnail',
                                        'author',
                                        'page-attributes',),
		)
	);
}
add_action( 'init', 'coupon_posttype' );

How to display Custom Post Types on Your Site

WordPress comes with built-in support for displaying custom post types. Once you added the code or created the custom post type with the plugin, you can start accessing it with the slug you provided.

For example, if you provided a ‘coupons’ slug for your coupon custom post type, you can access it like this:

http://example.com/coupons

Here, replace example.com with your website’s domain name.

You can also create a dedicated archive and a single page for your custom post type. For this, you just need to copy your theme’s archive.php file into archive-coupons.php and put the code for an archive. You can also create a single-coupons.php file to put the single-page code.

It is worth noting that custom post types will not come in between your regular posts. This is its primary advantage of it.

I have already written an article on how to include custom post types in blog’s RSS feed. You can also create a separate feed for each custom post type.

There could be many more things you can do. Covering everything in a single article is not possible. If you have anything specific to ask, you can comment below.

Final Words

I didn’t go into deep because there could be lot more things including the styling and theming. I just tried to explain how you can create a custom post type in WordPress to manage your content in a better way or creating a different kind of website. Custom Post Types are extremely useful and you can take advantage of it to create a website for real estate, movies, coupons, jobs and more.

I hope this post helps. If you have anything to ask, you can comment below.


Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

© 2022 The WP Guides Developed By Deepanker