A Complete Guide to Create WordPress Custom Post Type

Table of Content

One of the best things about WordPress is its customizability. You can display a host of content on your website with ease in a structured manner. However, despite its extensive array of features and plugins, you may want to break free from the pages and posts in WordPress. That’s why WordPress developers create custom post types, a game-changing feature to tailor the CMS to your unique needs and ambitions.

Whether it’s for a photography portfolio, a real estate listing site, an events calendar, or anything else, custom post types can help organize, display, and manage content in a better way. Now, while this topic can be a little confusing for some, the custom post types in WordPress bring something to your website worth the wait. 

In this guide to creating custom post types in WordPress, we’ll cover how they turn your WordPress website into a versatile platform capable of handling a variety of content types and purposes.

What are Custom Post Types in WordPress?

Custom post types are among the best elements propagating the customizability of WordPress. They let you define and showcase different types of content on your WordPress website. While WordPress comes with default content types like posts and pages, custom post types offer a means to go beyond these standards. 

With custom post types, you can tailor the website according to your specific requirements. With the ability to define custom fields, taxonomies, and templates for each post type, WordPress users can craft a highly customized and efficient content management system that precisely matches their content’s unique requirements. This adaptability is what sets WordPress apart as a versatile platform that can accommodate a vast range of website types and purposes.

Creating a WordPress custom post type is a process that involves defining its unique characteristics to suit your content needs. That includes specifying its name, labels, and configuring support for various features like thumbnails, custom fields, and hierarchical organization.

Once you’ve configured these characteristics and features, you’ll typically use code or a plugin to register the custom post type within your WordPress website. After registration, you can start creating and managing content using your custom post type, and it will have its own distinct section in the WordPress admin dashboard for easy access and organization.

Why Create Custom Post Types in WordPress?

Custom Post Types provide a way to organize and display various types of content on your website, making WordPress a highly versatile platform for different purposes.

Here’s why you should create WordPress custom post types:

Content Organization: Custom Post Types allow you to structure and organize your website’s content more effectively. Instead of trying to fit all your content into standard post or page formats, you can create distinct content types tailored to your needs. For instance, you could create custom post types for products, events, portfolio items, testimonials, and more.

Content Management: With custom post types, you can define unique fields and attributes specific to each content type. That lets you store and manage content in a way that makes sense for the particular type of information you’re handling. For instance, if you have a “Real Estate Listings” custom post type, you can include fields for property details like price, location, and number of bedrooms.

Improved User Experience: Custom Post Types enhance the user experience on your website. They make it easier for both site visitors and administrators to find and interact with specific types of content. It can result in a more engaging and user-friendly website.

Custom Templates and Designs: You can create custom templates for each post type, giving them unique layouts and designs. This level of customization allows you to showcase different types of content in the most visually appealing and functional way.

Plugin Compatibility: Many WordPress plugins and themes are designed to work seamlessly with custom post types. That means you can extend your website’s functionality and appearance further by leveraging existing WordPress resources.

SEO Benefits: Custom Post Types can help improve your website’s search engine optimization (SEO). By structuring your content properly, search engines can better understand and index your site, potentially leading to better search rankings.

The flexibility to define and customize custom post types is just one of the things that makes WordPress an incredibly adaptable platform. That’s why a wide range of websites opt for it. Next, we’ll see the elements of custom post types in WordPress that contribute to this flexibility.

Elements of a WordPress Custom Post Type

When learning how to create custom post types in WordPress, you have to understand the three crucial elements involved: $args, $labels, and $supports. They define and configure how your custom post type functions and appears on your website.

$args Array

The $args array, short for “arguments,” is used to configure various settings and behaviors of your custom post type. 

  • description: This parameter allows you to provide a brief description of your custom post type. It helps document the purpose of your post type and can be used as a reference.
  • public: A boolean value (true/false) that determines whether the custom post type is publicly accessible on your website. If set to ‘true’, it means the post type is visible to website visitors; if set to ‘false’, it’s not.
  • hierarchical: This parameter defines whether the post type should be hierarchical, similar to pages. If set to ‘true’, your custom post type can have parent-child relationships, making it suitable for content structures that require nesting.
  • exclude_from_search: A boolean value that controls whether the content of this post type should be included in WordPress’s search results. If set to ‘true’, the post type’s content will not appear in search results.
  • publicly_queryable: Specifies whether the post type can be queried publicly, meaning it can be accessed via URL queries. If set to ‘false’, it won’t be publicly accessible.
  • show_ui: Determines whether the post type should be displayed in the WordPress admin user interface (UI). Setting this to ‘true’ enables users to manage this post type through the admin panel.
  • show_in_nav_menus: This parameter indicates whether the post type should appear in the navigation menus. Enabling this allows you to add items of this post type to your site’s menus, making it easily accessible to visitors.
  • show_in_rest: Controls whether the post type should be displayed in the WordPress REST API. If set to ‘true’, the post type’s content can be retrieved and manipulated using REST API endpoints.
  • rest_base: Specifies the base slug for REST API requests related to this post type. It helps define the structure of API URLs for this content type.
  • rest_namespace: The namespace for REST API requests. It helps avoid conflicts when using the REST API for multiple custom post types or resources.
  • rest_controller_class: Specifies the controller class to use for handling REST API requests related to this post type. You can create a custom controller class if needed.
  • menu_position: Sets the position in the WordPress admin menu where this post type should appear. You can specify a numerical value to determine its location.
  • menu_icon: Allows you to set a custom icon for your post type in the WordPress admin menu. It enhances visual identification.
  • capability_type: Defines the base capability type for this post type, which influences user permissions and access control.
  • capabilities: Customizes the capabilities required to manage this post type. You can fine-tune who can perform specific actions, such as editing, deleting, or publishing posts of this type.
  • map_meta_cap: A boolean value that determines whether to map meta capabilities. When set to true, it ensures that fine-grained user capabilities are mapped correctly.
  • register_meta_box_cb: A callback function that registers meta boxes for your custom post type. Meta boxes are used to collect and display additional information for posts of this type.
  • taxonomies: An array of taxonomy names that should be associated with this post type. This allows you to categorize and tag your custom posts.
  • rewrite: Defines the URL structure for your custom post type’s archives and single items. It enables you to create clean and SEO-friendly URLs for your content.
  • can_export: Specifies whether content of this type can be exported. Useful for data portability.
  • delete_with_user: Determines if posts should be deleted with the user who created them. Useful for content ownership management.
    • When set to ‘true’, if a user’s posts of this type are associated with them, those posts will be transferred to the Trash when the user’s account is deleted.
    • When set to ‘false’, posts of this type that belong to the user will neither be moved to the Trash nor deleted when the user’s account is removed.
    • If left unspecified, which is the default setting, the behavior depends on whether the post type supports the ‘author’ feature. If it does, posts will be trashed upon the user’s deletion; otherwise, posts will neither be trashed nor deleted.
    • Default null.
  • has_archive: A boolean value defining the custom post type archives, along with the slug name and URL structure, based on the parameter 1 of register_post_types(). Archives are typically used to group and display posts by date or taxonomy terms.
  • template: This parameter is an array of blocks that help define the default initial state of an editor session. With ‘template’, you can preset specific block structures and content for when a post type is created. Every item created with ‘template’ should be an array with block name and optional attributes (defining the initial content, settings, or configuration for the block).
  • template_lock: Determines whether the template should be locked for editing to prevent accidental changes.
    • When set to ‘all’, the user is restricted from adding new blocks, moving existing blocks, and removing blocks.
    • If configured as ‘insert’, users have the ability to rearrange existing blocks but cannot add new ones or delete existing ones.
    • Default false. (So these restrictions aren’t applied by default.) 
  • show_in_admin_bar: Controls whether the post type should be shown in the WordPress admin bar.
  • show_in_nav_menus: Specifies whether the post type should appear in navigation menus. Similar to the ‘show_in_nav_menus’ parameter mentioned earlier, but defined within the ‘$ args’ array.
  • query_var: The query variable used to access this post type’s posts in URL queries. It influences how posts of this type are retrieved via URLs.
  • _builtin: A boolean value indicating whether the post type is a built-in WordPress post type (e.g., ‘post’ or ‘page’). Custom post types are typically not built-in. (It’s usually for INTERNAL USE ONLY)
  • _edit_link: The custom edit link for this post type which can be defined to override the default edit link. (It’s usually for INTERNAL USE ONLY)

Each of these parameters provides a specific aspect of control and customization for your custom post type, allowing you to fine-tune how it behaves, appears, and interacts with both users and the WordPress platform.

Along with $args, there are $labels and $supports containing various key and value pairs. Our WordPress experts create a separate $labels variable containing all the key and value pairs. That helps make the code cleaner and better comprehensible.

Let’s see what the $labels array is all about.

$labels Array

This array lets you specify how the custom post type and its various actions and elements are labeled in the WordPress admin dashboard. That makes it more user-friendly and intuitive for the administrators and content creators. 

  • name: This represents the plural name of your custom post type. It’s what users will see when referring to multiple items of this post type. For example, “Books” for a custom post type that represents books.
  • singular_name: This is the singular name of your custom post type used when referring to a single item. For instance, “Book” for an individual item in the “Books” post type.
  • add_new: The label used for adding a new item of this post type. It typically appears as a button or link in the WordPress admin interface, such as “Add New Book” for a book-related custom post type.
  • add_new_item: A more detailed description of adding a new item. It provides additional context when users click on the “Add New” button.
  • edit_item: This label is used when editing an individual item within your custom post type. It helps users understand the purpose of the editing screen.
  • new_item: Similar to add_new_item, this label is displayed when adding a new item, emphasizing the action of creating something new.
  • view_item: This label is used when viewing an individual item within your custom post type. It helps users navigate to the viewing screen.
  • search_items: Describes the action of searching for items within your custom post type. It’s used to indicate the search functionality.
  • not_found: A message displayed when no items are found in your custom post type. For example, “no books found” if there are no books in the “Books” post type.
  • not_found_in_trash: Similar to ‘not_found’, this message is displayed when no items are found in the Trash for your custom post type. It informs users that no trashed items exist.

$labels array can help make your custom post type more user-friendly and tailored to your specific CMS needs. You’ll have a smoother and more intuitive experience customizing your website. So make sure you understand $labels if you want to learn how to create custom post types in WordPress. For that, you may ask dedicated WordPress developers for help. 

$supports Array

The $supports array specifies which features and functionalities should be supported for a custom post type. It contains a list of feature names corresponding to various content elements and editing capabilities.

Let’s discuss the common ones.

  • title: This feature supports the title field for posts of this type, allowing you to specify a title for each post.
  • editor: Enabling this feature provides the post content editor, which allows you to input and format text, images, and other media within your posts.
  • author: When this feature is supported, you can assign an author to each post, indicating who created or contributed to the content.
  • thumbnail: Supporting thumbnails (also known as featured images) allows you to set a featured image for each post, which can be displayed prominently, such as in post listings.
  • excerpt: Enabling this feature provides an excerpt field, which allows you to create a brief summary or teaser for each post.
  • trackbacks: When supported, trackbacks (notifications from other websites linking to your posts) can be managed within the post editor.
  • custom-fields: This feature allows you to add and manage custom fields for posts, providing a way to store additional information or metadata.
  • comments: Supporting comments enables user-generated comments on posts, and it includes the comment management interface for post moderation.
  • revisions: Enabling revisions allows you to track and revert changes made to posts over time, providing a version history for each post.
  • page-attributes: When this feature is supported, additional options for page attributes, such as setting a parent page (for hierarchical custom post types), become available.
  • post-formats: Supporting post formats allows you to assign specific formats (e.g., standard, video, audio) to individual posts, which can affect how they are displayed.

You can choose the features relevant to your website’s content and objectives. That helps create a tailored experience within the WordPress admin interface for managing posts of that type.

These three elements are critical for custom post types. So, if you have any doubts about how to create custom post types using $args, $labels, and $supports, we recommend you opt for our WordPress development services

Code for Creating Custom Post Types in WordPress

Now that you understand the crucial elements involved in creating custom post types in WordPress, we can move forward to the code excerpt. The code for the same will be included in the functions.php file of a WordPress child theme on your website.

Let’s check out a code example on how to create custom post types in WordPress. In this instance, the custom post type name is “Portfolios”.

function custom_post_type_portfolios() {
    $labels = array(
        'name'               => __( 'Portfolios', 'text-domain' ),
        'singular_name'      => __( 'Portfolio', 'text-domain' ),
        'menu_name'          => __( 'Portfolios', 'text-domain' ),
        'add_new'            => __( 'Add New', 'text-domain' ),
        'add_new_item'       => __( 'Add New Portfolio', 'text-domain' ),
        'new_item'           => __( 'New Portfolio', 'text-domain' ),
        'edit_item'          => __( 'Edit Portfolio', 'text-domain' ),
        'view_item'          => __( 'View Portfolio', 'text-domain' ),
        'search_items'       => __( 'Search Portfolios', 'text-domain' ),
        'not_found'          => __( 'No portfolios found.', 'text-domain' ),
        'not_found_in_trash' => __( 'No portfolios found in Trash.', 'text-domain' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'portfolio' ), // Customize the URL slug
        'has_archive'        => true, // Enables post type archives
        'hierarchical'       => false,
        'menu_position'      => 5, // Adjust the menu position in the admin menu
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
        'capability_type'    => 'post',
        'publicly_queryable' => true,
    );
    register_post_type( 'portfolio', $args );
}
add_action( 'init', 'custom_post_type_portfolios' );

If your query is “How to create custom post types in WordPress?”, this code is your simple answer. It defines the labels and arguments for the custom post type “Portfolios” in the $labels and $args arrays, respectively. 

The ‘register_post_type()’ function is called with the post type name (“portfolio”) and the arguments to register the custom post type. We also add an activation hook to flush the rewrite rules upon the activation of the plugin. It ensures that the new post type’s URLs work correctly.

This code is completely customizable. So you can modify the labels, arguments, and supports according to your specific requirements for the custom post type. In this case, once you add this code to the functions.php file of your theme or custom plugin, the “Portfolios” custom post type will be available in your website’s WordPress admin panel. So you can create and manage portfolio-related content for your website more effectively. 

Plugins for Creating Custom Post Types in WordPress

As with anything else in this CMS, you can bypass the process of how to create custom post types in WordPress and go straight to the plugins. Not only can they simplify the process, but these plugins can also provide a steady, user-friendly interface for managing the post types. 

Here are a few of the most popular plugins for creating custom post types in WordPress. 

1. Custom Post Type UI

Custom Post Type UI is an immensely popular and user-friendly plugin that simplifies the process of creating custom post types and taxonomies. With its intuitive interface, it’s an ideal choice for beginners and those who want to avoid coding. 

You can define custom post types, set labels, configure options, and even export your settings for use in other projects. With 1M+ active installations, this plugin streamlines the process and is widely trusted in the WordPress community.

2. Toolset

Toolset is a powerful WordPress plugin suite that offers a variety of features for creating and managing custom post types, custom fields, and custom taxonomies. This plugin is a comprehensive solution for simplifying complex CMS needs. That makes it useful for WordPress website experts and site builders.

This plugin lets you create custom post types without needing to write code. You can define the post type’s structure, labels, and settings through an intuitive visual interface. Toolset is compatible with popular multilingual plugins like WPML. That makes it a viable choice for creating multilingual websites with custom content types.

3. Meta Box – WordPress Custom Fields Framework

Used for creating and managing custom post types and custom fields, Meta Box is a popular choice among website developers for extensive customizations. You can define custom post types using Meta Box. It lets you structure your content in a way that best suits your website’s needs. It’s a trustworthy plugin, as can be seen by its 700K active installations. 

With Meta Box, you can create custom post types for products, events, portfolios, testimonials, and more. Moreover, this plugin can be integrated with Gutenberg editor, along with popular page builder plugins like Elementor. 

4. JetEngine

JetEngine is a high-quality plugin with a feature that allows you to easily create and manage custom post types and taxonomies. This feature is essential for structuring and organizing different types of content on your website, such as portfolios, directories, listings, and more.

While JetEngine is user-friendly for non-developers, it also provides extensive developer features, including hooks, filters, and APIs. So they can extend and customize its functionality further.

5. Custom Post Types and Custom fields Creator – WCK

As the name suggests, the Custom Post Types and Custom Fields Creator – WCK is a versatile plugin used to create custom post types, custom fields, and custom taxonomies with ease. This plugin also has a visual builder that simplifies the custom post type creation using a user-friendly drag-and-drop interface.

Along with the custom post types, WCK supports the creation and management of custom taxonomies. With this feature, you can create hierarchies and apply taxonomies to custom post types for organizing and categorizing content more effectively. 

These plugins cater to different user levels and project complexities, providing options for everyone from beginners to advanced developers. So you can choose one according to your website needs and familiarity with WordPress development. 

Or, if you want more personalized features related to custom post types on your website, opt for our WordPress plugin development services. Our developers will help create a custom plugin according to your website content requirements. 

That should, once and for all, answer the question “How to create custom post types in WordPress?”.

FAQs Related to Creating WordPress Custom Post Types

Why would I need a custom post type in WordPress?
Custom post types are useful when you want to organize and display different types of content separately on your website. For example, you might create custom post types for products, events, testimonials, portfolio items, or any content type that doesn't fit into the default "posts" or "pages" categories.
What are custom fields, and how can I add them to my custom post type?
Custom fields are additional data fields you can associate with your custom post type to store specific information. You can add custom fields to your custom post type either through code or by using plugins like Advanced Custom Fields or the built-in custom field functionality in WordPress.
Can I customize the labels and attributes of my custom post type?
Yes, you can customize various aspects of your custom post type, including labels (such as name, singular name, and menu name), attributes (such as hierarchical structure and support for custom fields), and more using the available settings or code.

Conclusion

When creating or customizing a website, one of the significant steps taken by any WordPress development agency is to create WordPress custom post types. It lets them structure and showcase various types of content beyond the default posts and pages. 

Like any other feature or functionality, there are two ways to create custom post types in WordPress. You can either write the code and integrate it into a custom theme or plugin or simply opt for a custom post types plugin, like Custom Post Type UI, Toolset, Meta Box, or others.

If you have any other query on how to create custom post types in WordPress or want to customize your website, consult our experts today!

author
Jigar Shah is the Founder of WPWeb Infotech - a leading Web Development Company in India, USA. Being the founder of the company, he takes care of business development activities and handles the execution of the projects. He is Enthusiastic about producing quality content on challenging technical subjects.

Leave a comment