How to Create Custom Post Types (CPT) in WordPress

Introduction

By default, WordPress provides two main content types: Posts and Pages. While these are enough for simple websites, many projects require additional content types.

For example, a website may need to manage:

  • Portfolio items
  • Testimonials
  • Products
  • Events
  • Tutorials

To organize this type of content properly, WordPress allows developers to create Custom Post Types (CPTs).

Custom Post Types allow you to create new content sections in the WordPress dashboard that behave similarly to posts and pages but are designed for specific purposes.

In this tutorial, you will learn what Custom Post Types are and how to create them step by step.

What is a Custom Post Type in WordPress?

A Custom Post Type (CPT) is a content type created by developers to store and manage specific kinds of data in WordPress.

Examples of common post types in WordPress include:

Post TypePurpose
PostBlog articles
PageStatic pages
AttachmentMedia files
Custom Post TypeCustom content like portfolio or products

For example, if you are building a portfolio website, instead of storing projects as blog posts, you can create a Portfolio post type.

This keeps your content organized and easier to manage.

When Should You Use Custom Post Types?

Use CPT when:

  • You want separate content types
  • Blog posts are not enough
  • You are building:
    • Portfolios
    • Business websites
    • Learning platforms
    • Job listings
    • Case studies

How Custom Post Types Work

Custom Post Types are registered in WordPress using the function:

<?php register_post_type(); ?>

This function tells WordPress to create a new content type and display it in the admin dashboard.

Once registered, the new post type appears in the WordPress menu just like Posts or Pages.

Step 1: Register Custom Post Type (Code Method)

Open your theme’s functions.php file and add this code:

<?php
function div_register_projects_cpt() {    $labels = array(
        'name'               => 'Projects',
        'singular_name'      => 'Project',
        'add_new'            => 'Add New Project',
        'add_new_item'       => 'Add New Project',
        'edit_item'          => 'Edit Project',
        'new_item'           => 'New Project',
        'view_item'          => 'View Project',
        'search_items'       => 'Search Projects',
        'not_found'          => 'No projects found',
        'not_found_in_trash' => 'No projects found in Trash',
        'menu_name'          => 'Projects'
    );    $args = array(
        'labels'        => $labels,
        'public'        => true,
        'has_archive'   => true,
        'rewrite'       => array('slug' => 'projects'),
        'supports'      => array('title', 'editor', 'thumbnail'),
        'show_in_rest'  => true // for Gutenberg support
    );    register_post_type('project', $args);
}add_action('init', 'div_register_projects_cpt');
?>

Understanding the Code

Let’s understand what the code is doing.

register_post_type(‘project’)

This defines the internal name of the Custom Post Type.

labels

Labels define the names used in the WordPress admin dashboard.

Example:

  • Project
  • Add New Project
  • Edit Project

public => true

This makes the Custom Post Type visible on the website and in the admin panel.

has_archive => true

Allows WordPress to generate an archive page like:

yoursite.com/project

supports

Defines what features the post type supports.

Common options include:

  • title
  • editor
  • thumbnail
  • comments
  • custom-fields

Step 2: Check CPT in WordPress Dashboard

After saving:

  • Go to WordPress Admin
  • You’ll see a new menu: Projects
  • Click Add New and create your first Project

Step 3: Display Custom Post Types on Frontend

Basic example to show CPT posts:

<?php 
$args = array(
    'post_type' => 'project',
    'posts_per_page' => 5
);$query = new WP_Query($args);if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        echo '<h2>' . get_the_title() . '</h2>';
        the_excerpt();
    }
}wp_reset_postdata();
?>

Step 4: Enable Featured Image for CPT

Already done using:

&lt;?php 'supports' => array('title', 'editor', 'thumbnail'); ?

Now you can upload images for each Project.

Example Use Cases for Custom Post Types

Custom Post Types are widely used in real-world WordPress projects.

Some examples include:

Portfolio projects
Customer testimonials
Products in an eCommerce system
Team members
Events and webinars

Using Custom Post Types keeps each type of content separate and organized.

Creating Custom Post Types Using Plugins

If you don’t want to write code, you can create Custom Post Types using plugins.

Popular plugins include:

  • Custom Post Type UI
  • Advanced Custom Fields

These plugins provide an interface where you can create Custom Post Types without coding.

However, developers usually prefer the code method because it gives more control.

Best Practices When Creating Custom Post Types

Follow these best practices when working with CPTs:

Use singular names for the internal slug
Choose meaningful labels
Keep post types focused on a single purpose
Use custom templates for better design
Avoid creating too many post types unnecessarily

These practices keep your WordPress project clean and maintainable.

Example URL Structure

If your Custom Post Type is portfolio, WordPress may generate URLs like:

yoursite.com/portfolio/project-1
yoursite.com/portfolio/project-2

This improves website organization and SEO.

Common Beginner Mistakes

Beginners often make these mistakes when creating Custom Post Types:

Adding CPT code in the wrong file
Forgetting to refresh permalinks
Using incorrect slug names
Creating too many unnecessary post types

Always test your CPT after creating it.

Practical Tasks

To strengthen your understanding of Custom Post Types in WordPress, try completing the following tasks.

Task 1: Create a Portfolio Custom Post Type

Create a Custom Post Type named Portfolio using the register_post_type() function.

Make sure it includes the following features:

  • Title
  • Editor
  • Featured Image

After adding the code, refresh the WordPress dashboard and confirm that the Portfolio menu appears.

Task 2: Add Portfolio Items

Add at least three portfolio items such as:

  • Website Design Project
  • Mobile App Project
  • Landing Page Project

Fill in the title and description for each item.

Task 3: Enable Archive Page

Make sure the following parameter is enabled in your CPT code:

'has_archive' => true

Then visit the archive page in your browser:

yoursite.com/portfolio

Check whether the portfolio items are displayed.

Task 4: Customize Supported Features

Modify the supports parameter to include additional features such as:

  • Excerpt
  • Comments
  • Custom Fields

Example:

'supports' => array('title', 'editor', 'thumbnail', 'excerpt')

Save the file and confirm that these options appear when editing a Portfolio item.

Task 5: Explore Real-World Use Cases

Think about a website where Custom Post Types could be useful. For example:

  • Real estate website (Properties)
  • School website (Courses)
  • Business website (Testimonials)

Try designing a Custom Post Type structure for one of these ideas.

Conclusion

Custom Post Types are one of the most powerful features of WordPress development. They allow you to create structured content types beyond regular posts and pages.

By using the register_post_type() function, developers can build flexible systems for managing portfolios, products, events, and many other types of content.

Understanding Custom Post Types is an important step toward becoming an advanced WordPress developer.

In the next tutorial, we will learn about Custom Fields with Meta Boxes in WordPress and how they help to store and show custom data.

Related Tutorials

Leave a Reply

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