Press ESC to close

How to Create Custom Post Types in WordPress

Creating custom post types (CPTs) in WordPress is a powerful way to organize and present content that doesn’t fit into the default “Post” or “Page” structure. Custom post types allow you to define new content types with their own unique characteristics, such as custom fields, taxonomies, and templates. In this guide, we’ll walk you through how to create custom post types in WordPress, step by step.

What is a Custom Post Type?

In WordPress, a post type is a specific type of content, such as posts, pages, or attachments. WordPress has a variety of built-in post types, such as:

  • Post: Standard blog posts.
  • Page: Static content like “About” or “Contact” pages.
  • Attachment: Media files like images, PDFs, etc.
  • Revision: Versions of posts or pages.
  • Nav Menu Item: Items in a navigation menu.

A Custom Post Type (CPT) is any post type you define to store specific types of content, such as portfolios, events, testimonials, or products. Custom post types give you flexibility in how content is structured and displayed on your WordPress website.

Why Create Custom Post Types?

Here are a few reasons you might want to create custom post types in WordPress:

  • Organization: Helps in organizing different types of content (e.g., separating events from regular blog posts).
  • Flexibility: Custom post types can have custom fields, taxonomies, and templates that are specific to that type of content.
  • Functionality: By creating custom post types, you can easily integrate new features such as product listings, event management, or portfolios.

Step-by-Step Guide to Creating Custom Post Types in WordPress

There are two main ways to create custom post types in WordPress:

  1. Using a Plugin (Easy Method)
  2. Manually Registering a Custom Post Type (Advanced Method)

We will walk you through both methods.

Method 1: Using a Plugin (Easy Method)

If you’re not comfortable with coding, using a plugin is the easiest way to create custom post types. One of the most popular plugins for this is Custom Post Type UI.

Steps to create a Custom Post Type using Custom Post Type UI plugin:

  1. Install and Activate the Plugin:
    • From your WordPress dashboard, go to Plugins > Add New.
    • Search for “Custom Post Type UI” and install it.
    • Activate the plugin.
  2. Create a Custom Post Type:
    • Once the plugin is activated, go to CPT UI > Add/Edit Post Types from the left-hand menu.
    • Under the “Add New Post Type” section, fill out the required fields:
      • Post Type Slug: A unique identifier for your post type (e.g., portfolio, events, testimonials).
      • Plural Label: The plural name for your post type (e.g., “Portfolios”, “Events”).
      • Singular Label: The singular name (e.g., “Portfolio”, “Event”).
    • Under the “Settings” tab, customize other options such as visibility, supports (like title, editor, thumbnail, etc.), and rewrite rules.
  3. Save the Custom Post Type:
    • Click the “Add Post Type” button to save your new custom post type.
  4. Create Custom Posts:
    • After creating the post type, you’ll see it appear as a menu item in your WordPress dashboard. You can now start adding posts under this new custom post type by going to the menu and clicking Add New.

This method is quick and user-friendly, especially if you’re not familiar with writing code. The plugin also gives you an interface to manage custom post types, which makes it easier to modify settings later on.

Method 2: Manually Registering a Custom Post Type (Advanced Method)

If you’re comfortable with coding and want more control, you can register a custom post type manually using WordPress’s register_post_type() function. This gives you more flexibility and allows you to define advanced options.

Steps to manually create a Custom Post Type:

  1. Add Code to Your Theme’s functions.php File:
    • Go to Appearance > Theme Editor and select your theme’s functions.php file.
    • Alternatively, you can use a child theme to avoid losing changes during theme updates.
  2. Write the Code to Register Your Custom Post Type:

Here’s an example of how to register a custom post type for a portfolio:

function create_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Portfolios',
'singular_name' => 'Portfolio',
'add_new' => 'Add New Portfolio',
'add_new_item' => 'Add New Portfolio Item',
'edit_item' => 'Edit Portfolio Item',
'new_item' => 'New Portfolio Item',
'view_item' => 'View Portfolio Item',
'search_items' => 'Search Portfolios',
'not_found' => 'No portfolios found',
'not_found_in_trash' => 'No portfolios found in Trash',
'all_items' => 'All Portfolios',
'archives' => 'Portfolio Archives',
'insert_into_item' => 'Insert into portfolio item',
'uploaded_to_this_item' => 'Uploaded to this portfolio item',
'menu_name' => 'Portfolios'
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'portfolio'),
'show_in_rest' => true, // Enable for Gutenberg editor
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
'taxonomies' => array('category', 'post_tag'), // Optional: add taxonomies
'menu_icon' => 'dashicons-portfolio', // Optional: choose a custom menu icon
);

register_post_type('portfolio', $args);
}

add_action('init', 'create_custom_post_type');

Breakdown of the Code:

  • labels: These define the names and text displayed in the WordPress dashboard for the custom post type.
  • public: If set to true, this makes the post type accessible from the frontend.
  • has_archive: If set to true, this enables an archive page for the post type.
  • rewrite: This allows you to customize the URL slug (e.g., yoursite.com/portfolio/).
  • supports: This array lists the features that are enabled for the custom post type, such as title, editor, and thumbnail.
  • menu_icon: This sets a custom icon for the post type in the WordPress dashboard menu.
  1. Save the Changes:
    • After adding the code to functions.php, save the file.
  2. Create Posts Under Your Custom Post Type:
    • You should now see a new menu item in your WordPress dashboard for the custom post type you just created (in this case, “Portfolios”).
    • Click Add New under this menu item to start adding new content for your custom post type.

Additional Customizations for Custom Post Types

You can customize your custom post types further by adding custom taxonomies (e.g., genres, types), custom fields, and creating custom templates.

1. Custom Taxonomies:

You can register custom taxonomies (similar to categories and tags) for your custom post type. For example, adding a “Skills” taxonomy for a portfolio:

function create_custom_taxonomy() {
$args = array(
'labels' => array(
'name' => 'Skills',
'singular_name' => 'Skill',
'search_items' => 'Search Skills',
'all_items' => 'All Skills',
'edit_item' => 'Edit Skill',
'update_item' => 'Update Skill',
'add_new_item' => 'Add New Skill',
'new_item_name' => 'New Skill Name'
),
'hierarchical' => true, // Set to true for categories-style taxonomy, false for tags-style.
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true, // Enable for Gutenberg
);

register_taxonomy('skill', 'portfolio', $args);
}

add_action('init', 'create_custom_taxonomy');

2. Custom Templates:

You can create custom templates for your custom post types by creating a template file like single-portfolio.php (for individual posts) or archive-portfolio.php (for the archive page).

Conclusion

Creating custom post types in WordPress allows you to manage and display different types of content in a structured way, improving both the usability and functionality of your site. Whether you choose the easy route with plugins or go for a more customized approach by coding it yourself, custom post types are a great tool for building specialized content.