Custom Post Types in WordPress: Everything You Need to Know and How to Create One!

Custom Post Types in WordPress: Everything You Need to Know and How to Create One!

WordPress post types turn WordPress into a full-scale CMS (Content Management System). If youā€™re new to post types and want to learn about it from scratch, read: everything about WordPress post types here!

Custom post types in WordPress have truly changed the CMS game for WordPress. It gives an extra room for flexibility and features that enables you to do more with WordPress. Now, you can create and extend your website(s) to create specialized digital spaces for your visitors, customers, and users.

Letā€™s delve deeper into WordPress post types, templates, kinds, etc. Take a dive into WordPress and understand how it evolved WordPress to cater to much bigger and more diverse users and audiences.

Why do I Need a Custom Post Type (CPT)?

You mustā€™ve read about post types and their needs in the previous article. Creating custom post types enables you to show different content on your website that go beyond the standard format like a post, page, etc.

For example, if you run a website that talks about different recipes, then youā€™d want to create a recipe post type. With custom post types there is no stopping you from creating any kind of website, whether it be personal or professional. As it will enable you to create custom post types for portfolios, research, services, movie reviews, testimonials, etc.

So, primarily It helps to better organize the content on your website. Also, makes it easier for you to update, edit, and add more content to different post types, thus maintaining content diversity on your website.

Custom post types can have different custom fields and their own custom category structure.

Many popular WordPress plugins use custom post types to store data on your WordPress website. The following are a few top plugins that use custom post types:

Do I Need Custom Post Types?

Before venturing into a customized WordPress experience, the first task is to figure if your website actually needs a custom post type (s). Because believe me, the maximum results can be achieved with default or built-in post types that come with WordPress!

A custom post type is nothing more than a regular post with a different post_type value in the database. The post type of regular posts is post, pages use page, attachments use the attachment, and so on. Create your own to indicate the type of content created. You could create custom post types for books, industries, research, case studies, reviews, services, etc.

You can achieve the following:

  • The custom post type will show up in the back end as a separate menu item with its own post list and ā€œadd newā€ page
  • Navigating to https://mysite.com/customposttype/ will take you to the archive page for the post type. This is akin to visiting the front page for the latest posts from the ā€œpostā€ post type.
  • Categories and tags can be made available to the custom post type, or you can create custom taxonomies.

Apart from these, you also have the freedom to modify it as you like, right from its placement in the menu, which user level can access, whether it should be hierarchical or not, you can modify countless options, such as where the custom post type should be placed in the menu, should it be searchable, which user level can access it, should it be hierarchical, custom rewrite rules, etc.

How to Create Custom Post Type with a Plugin?

WordPress makes every job very simple and easy. How?

All praises go to plugins. There are currently around ~60,100 plugins in WordPress that are ready to provide you with all the features, functionalities, and customizations that you might think of or want on your WordPress website.

With so many plugins available for various features and functionalities, letā€™s just use them to get the most out of your website. Iā€™ll be using the Custom Post Type UI plugin. All you have to do is download and activate the plugin on your website, then read below to create your custom post type.

After activating the plugin, go to CPT UI > Add/Edit Post Types. You should be on the tab ‘Add New Post Type‘.

Image: Adding a custom post type with CPT UI

First, you need to provide a slug for your WordPress custom post type like, ā€˜research_papersā€™. Iā€™d highly recommend you read the instructions right below the box to make sure you create the right slug. It would help if you listed the plural and singular names for your custom post type.

If you want to skip past the formalities, you can select the “Populate additional labels based on chosen labels” option. You’ll save time by having the plugin automatically fill up the additional label fields below.

You can now scroll down to that area marked “Additional Labels.” Here, you need to give a description for your post type and modify labels. If you choose not to select the “Populate additional labels based on chosen labels” option.

Image: Adding additional labels in CPT UI

These fields are important for adding, editing, and manage material for your CPT in the WordPress user interface. These labels will be key to providing information that will be utilized everywhere.

The next step is to configure the post type. From this point, you can configure various post-type attributes. There is also a description as to what each option does, so you know exactly what youā€™re doing.

Image: Settings for custom post type in CPT UI

You can decide a lot of things to do with your CPT. For eg., to make a post type hierarchical or not like pages or to order chronological posts in the opposite order.

Below the general settings, in the support panel, you get to decide the type of editing features your custom post type will offer. Select the boxes next to the options you want to include the features.

Image: Support options for custom post type in CPT UI

Click the “Add Post Type” button to save and create your post type.

Voila! Youā€™ve successfully built your custom post type. Congratulations Sir/Madam!

How to Create a Custom Post Type Without a Plugin?

To create your custom post type in WordPress, the 2 main ways to do it are:

  • Creating CPT with a plugin
  • Adding a custom code to the theme’s functions.php

There is another way of creating a custom plugin and then have post types on your website. Iā€™ll soon be sharing a guide, so you can do that! Moreover, Iā€™ll be telling you why you would need to use this way to have CPT on your website.

To build a custom post type without a plugin, the first method is to create a CPT using the functions.php in your theme. Here’s an example of creating a custom post type called “Book” using the register_post_type() function in the theme’s functions.php:

/**
 * Register a custom post type called "book".
 *
 * @see get_post_type_labels() for label keys.
 */
function wpdocs_codex_book_init() {
	$labels = array(
		'name'                  => _x( 'Books', 'Post type general name', 'textdomain' ),
		'singular_name'         => _x( 'Book', 'Post type singular name', 'textdomain' ),
		'menu_name'             => _x( 'Books', 'Admin Menu text', 'textdomain' ),
		'name_admin_bar'        => _x( 'Book', 'Add New on Toolbar', 'textdomain' ),
		'add_new'               => __( 'Add New', 'textdomain' ),
		'add_new_item'          => __( 'Add New Book', 'textdomain' ),
		'new_item'              => __( 'New Book', 'textdomain' ),
		'edit_item'             => __( 'Edit Book', 'textdomain' ),
		'view_item'             => __( 'View Book', 'textdomain' ),
		'all_items'             => __( 'All Books', 'textdomain' ),
		'search_items'          => __( 'Search Books', 'textdomain' ),
		'parent_item_colon'     => __( 'Parent Books:', 'textdomain' ),
		'not_found'             => __( 'No books found.', 'textdomain' ),
		'not_found_in_trash'    => __( 'No books found in Trash.', 'textdomain' ),
		'featured_image'        => _x( 'Book Cover Image', 'Overrides the ā€œFeatured Imageā€ phrase for this post type. Added in 4.3', 'textdomain' ),
		'set_featured_image'    => _x( 'Set cover image', 'Overrides the ā€œSet featured imageā€ phrase for this post type. Added in 4.3', 'textdomain' ),
		'remove_featured_image' => _x( 'Remove cover image', 'Overrides the ā€œRemove featured imageā€ phrase for this post type. Added in 4.3', 'textdomain' ),
		'use_featured_image'    => _x( 'Use as cover image', 'Overrides the ā€œUse as featured imageā€ phrase for this post type. Added in 4.3', 'textdomain' ),
		'archives'              => _x( 'Book archives', 'The post type archive label used in nav menus. Default ā€œPost Archivesā€. Added in 4.4', 'textdomain' ),
		'insert_into_item'      => _x( 'Insert into book', 'Overrides the ā€œInsert into postā€/ā€Insert into pageā€ phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
		'uploaded_to_this_item' => _x( 'Uploaded to this book', 'Overrides the ā€œUploaded to this postā€/ā€Uploaded to this pageā€ phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
		'filter_items_list'     => _x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default ā€œFilter posts listā€/ā€Filter pages listā€. Added in 4.4', 'textdomain' ),
		'items_list_navigation' => _x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default ā€œPosts list navigationā€/ā€Pages list navigationā€. Added in 4.4', 'textdomain' ),
		'items_list'            => _x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default ā€œPosts listā€/ā€Pages listā€. Added in 4.4', 'textdomain' ),
	);

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
	);

	register_post_type( 'book', $args );
}

add_action( 'init', 'wpdocs_codex_book_init' );

This code creates a new custom post type “book” with the labels “Books” for the post type name and “Book” for the singular name.

Remember, you also need to flush the permalinks after adding this custom post type.

  • labels The labels option should be an array defining the different labels that a custom post type can have.
  • description A short explanation of our custom post type; what it does and why weā€™re using it.
  • public This option controls a bunch of things in one go. Setting this to true will set a bunch of other options (all to do with visibility) to true.
  • menu_position Defines the position of the custom post-type menu in the back end. Setting it to ā€œ5ā€ places it below the ā€œpostsā€ menu; the higher you set it, the lower the menu will be placed.
  • supports This option sets up the default WordPress controls that are available in the edit screen for the custom post type. By default, only the title field and editor are shown. If you want to add support for comments, revisions, post formats, and such you will need to specify them here.
  • has_archive If set to true, rewrite rules will be created for you, enabling a post-type archive at https://mysite.com/posttype/ (by default).

After setting this up, you should see the menu entry for the custom post type. You should be able to add posts, view the post list in the admin, and also visit the published posts on the website.

As I mentioned there are a lot of things you can modify when creating a post type. I suggest looking at the arguments list in the Codex for a full description of each option and the possible values.

You may create an unlimited number of custom post types. However, too many custom post types can slow the REST API discovery and complicate the admin bar.

WordPress Custom Post Type: Common Uses

The definition and need for a custom post type must be crystal clear to you now. In case you havenā€™t understood, feel free to comment or drop a mail šŸ˜‰. You must be wondering, just when and where would you need to create a CPT?

The answer is quite simple: You have to put up content on your website but it does not fit. When you want to add content to your website that doesn’t fit into one of the pre-built post categories!

Typical instances of custom post types include:

  • Research for an online store
  • Projects or portfolio pieces for a portfolio website
  • A mapping website’s maps
  • Events for a booking site

WordPress Custom post types are extremely helpful as they give WordPress the essential flexibility you need to build a custom website. Also, it is post types as a whole that transform it from a blogging tool to a full-scale hardcore CMS.

Conclusion

Because CPTs are separate from Posts and Pages, they can be displayed separately, organized by their own taxonomy categorizations, and filtered based on their custom fields.

The ability to create your own Custom Post Type enables you to use WordPress as a framework for building a website that fits your exact needs, opening up limitless possibilities for managing and displaying content.

This is everything you need to get started with post types and take complete control and command over your WordPress website. For any doubts, questions, or thoughts, drop a comment below. Subscribe to our newsletter to get the latest from WPWB, straight to your inbox! šŸ“¬

Discussion

No responses / comments so far.

Leave a Reply

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