How to Register Custom Post Types in WordPress: A Comprehensive Guide

WordPress is a flexible Content Management System (CMS) that allows users to create custom types of content using “Custom Post Types.” This useful feature allows WordPress site owners to create specialized post types, such as products, events, portfolios and more, each having its own unique set of fields.

In this guide, we will show you how to create custom post types in two ways, using plugins or code. We will also provide best practices to optimize custom post types for search engines and user experience.

Plugins vs. Code

When creating a custom post type in WordPress, you have two options: using plugins or code.

Plugins

Plugins are a popular way to create custom post types. They save time and make it easy for users without coding skills to create custom post types.

Two popular plugins are:

  1. Custom Post Type UI – a free plugin that allows you to create custom post types/taxonomies with an easy-to-use interface.
  2. Toolset Types – a premium plugin that enables you to create custom post types, custom fields, and taxonomies using a drag-and-drop interface.

Using a plugin can limit your ability to customize the custom post type as the plugin will have predefined settings. Moreover, some plugins may not be compatible with your WordPress theme or other plugins.

Code

Using code to create custom post types in WordPress gives you complete control over the functionality and appearance of the post type. By writing code, you can customize every aspect of the custom post type, including labels, capabilities, rewrite rules, and more.

If you prefer to use code, head to the Official WordPress documentation to learn more.

Best Practices for Custom Post Types

Here are some best practices for creating custom post types:

Choose The Right Post Types

Choose carefully what type of content you want to create for your WordPress site. Content should be easily understood by the users, and not confuse them. It is also essential to have a clean, organized structure. WordPress has several built-in post types, and creating a new one should have a good reason.

Optimize for Search Engines

Custom post types are optimized for search engines by default, but it is essential to customize them further for better ranking results. You can optimize custom post types by using keywords in their title, URLs, and meta descriptions. Custom fields and taxonomies are also useful for SEO purposes.

Optimize for User Experience

Providing an excellent user experience should be the primary goal of every website. Custom post types help with that by tailoring content types to the user’s needs. For instance, you can create a custom post type that is for events, where the user can search and sort them by date, location, or other custom taxonomies. Using custom images, providing concise category filters, and organizing the post types well can help improve user experience

How to Create Custom Post Types in WordPress

In this section, we will show you how to create custom post types in WordPress easily.

Using Plugins

Custom Post Type UI

  1. Install and activate “Custom Post Type UI” plugin
  2. Navigate to “Custom Post Types” > “Add New” from the WordPress dashboard
  3. Provide a name and description for the custom post type.
  4. Set options as required, including the menu position, exclude from search results, and more.
  5. Add custom taxonomies if needed, properties, and slugs.
  6. Save the custom post type.

Toolset Types

  1. Install and activate the “Toolset Types” plugin.
  2. Go to “Types” > “Post Types” from the WordPress admin panel.
  3. Choose “Add New Post Type”.
  4. Fill in the name, label, and description fields of the custom post type.
  5. Choose the capabilities for the custom post type, including edit, create, and delete.
  6. Provide the custom permalink structure.
  7. Add custom fields or taxonomies.
  8. Choose whether the post type will have an archive page.
  9. Save the custom post type.

Using Code

To create a custom post type using code, follow these steps:

  1. Access the “functions.php” file of your current WordPress theme.
  2. Add the following code:
function register_custom_post_type() {
    register_post_type( 'portfolio',
        array(
            'labels' => array(
                'name' => __( 'Portfolio' ),
                'singular_name' => __( 'Portfolio Item' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );
}
add_action( 'init', 'register_custom_post_type' );Code language: PHP (php)

This code creates a custom post type called Portfolio.

  1. Customize your custom post type by adding more parameters:
$args = array(
  'labels' => $labels,
  'description' => __( 'Description' ),
  'public' => true,
  'publicly_queryable' => true,
  'show_ui' => true,
  'show_in_menu' => true,
  'query_var' => true,
  'rewrite' => array( 'slug' => 'portfolio' ),
  'capability_type' => 'post',
  'has_archive' => true,
  'hierarchical' => false,
  'menu_position' => null,
  'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'portfolio', $args );Code language: PHP (php)
  1. Save the changes to the “functions.php” file and reload the WordPress admin dashboard.
  2. Check that the custom post type was added by going to the WordPress menu and look for the newly created custom post type.

Conclusion

Creating custom post types in WordPress allows users to tailor content to their specific needs. Using plugins or code, it is straightforward to create custom post types in WordPress. Ensure that you choose the right post types, optimize your URLs, and use custom images to provide excellent user experience. By following our guide, you too can create custom post types effortlessly.

Avatar photo
Morgan

I help eCommerce store owners to run their stores smoothly and get more sales. Let's discuss optimizing your store! Hit me up via the support page or on Twitter @morganhvidt