How to create custom post type Explained: Tips and Best Practices

# How to Create a Custom Post Type in WordPress: A Step-by-Step Guide

WordPress is a powerhouse for content creation, but its default “Posts” and “Pages” aren’t always the right fit for every type of content. What if you need a portfolio, a team directory, product listings, or event calendars? This is where **Custom Post Types (CPTs)** come in. They allow you to extend WordPress beyond blogging and build truly custom, structured websites. In this guide, you’ll learn exactly how to create a custom post type, both with code and plugins.

## What is a Custom Post Type?

At its core, a Custom Post Type is a content type you define that operates just like a standard post or page but is logically separated. Think of it as a new content category with its own unique characteristics. By default, WordPress has several post types: `post` (blog posts), `page` (static pages), `attachment` (media), and `revision`. A CPT adds a new one to this list, like `portfolio`, `testimonial`, or `product`.

Creating a CPT gives you dedicated admin menus, custom fields, and taxonomies (like categories and tags) tailored to your specific content. This leads to a cleaner backend for you and a more organized, powerful website.

## Method 1: Creating a Custom Post Type with Code (The Developer Way)

The most robust and performant method is to register your CPT directly in your theme’s `functions.php` file or, better yet, in a site-specific plugin. This gives you full control and keeps functionality intact if you change themes.

### Step 1: Access Your Theme’s Functions.php File

Navigate to **Appearance > Theme File Editor** in your WordPress dashboard, or use an FTP client to access your site files. Locate your active theme’s `functions.php` file.

### Step 2: Register the Custom Post Type

You’ll use the `register_post_type()` function. Let’s create a “Portfolio” CPT as an example.

“`php
function create_portfolio_post_type() {
$labels = array(
‘name’ => ‘Portfolio’,
‘singular_name’ => ‘Portfolio Item’,
‘menu_name’ => ‘Portfolio’,
‘add_new’ => ‘Add New Item’,
‘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 Portfolio’,
‘not_found’ => ‘No items found’,
‘not_found_in_trash’ => ‘No items found in Trash’
);

$args = array(
‘labels’ => $labels,
‘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’ => 20,
‘menu_icon’ => ‘dashicons-portfolio’,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’ )
);

register_post_type( ‘portfolio’, $args );
}
add_action( ‘init’, ‘create_portfolio_post_type’ );
“`

### Step 3: Understanding Key Arguments

Let’s break down some critical parameters in the `$args` array:

* **`public`:** Controls visibility in the admin and front end.
* **`rewrite`:** Defines the URL slug for your CPT (e.g., `yoursite.com/portfolio/item-name`).
* **`has_archive`:** If `true`, creates an archive page listing all your CPT items.
* **`menu_icon`:** Choose from WordPress Dashicons. This example uses `dashicons-portfolio`.
* **`supports`:** Specifies which features the CPT will use (title, editor, featured image, etc.).
* **`menu_position`:** Determines its position in the admin menu (5 = below Posts, 20 = below Pages).

After saving `functions.php`, you should see a new “Portfolio” menu in your WordPress admin sidebar.

### Step 4: Flush Permalinks

This is a crucial step! Go to **Settings > Permalinks** and simply click “Save Changes.” This refreshes the rewrite rules and ensures your new CPT URLs work correctly.

## Method 2: Creating a Custom Post Type with a Plugin (The User-Friendly Way)

If you’re not comfortable with code, several excellent plugins can handle CPT creation with a user interface. **Custom Post Type UI** and **Toolset Types** are popular choices.

Using Custom Post Type UI

  1. Install and activate the “Custom Post Type UI” plugin.
  2. Go to CPT UI > Add/Edit Post Types.
  3. Fill in the basic settings: Post Type Slug (e.g., “portfolio”), Plural Label, and Singular Label.
  4. Configure the many available settings in a visual form, mirroring the `$args` array from the code method.
  5. Click “Add Post Type.” The plugin handles the code registration for you.

The plugin method is faster and less error-prone for beginners, but it adds a dependency. If you deactivate the plugin, your CPT data remains safe in the database, but the registration code disappears unless you use the plugin’s “Get Code” feature to transfer it to your theme.

## Best Practices and Next Steps

Creating the CPT is just the beginning. To build a complete system, consider these next steps:

  • Create Custom Taxonomies: Add specific categories or tags for your CPT (e.g., “Portfolio Types” or “Client Names”).
  • Use Advanced Custom Fields (ACF): This powerful plugin lets you add custom meta boxes for structured data like client names, project dates, or URLs.
  • Build Custom Templates: To display your CPT uniquely, create template files in your theme like single-portfolio.php and archive-portfolio.php.
  • Keep Code Portable: Always add CPT code to a site-specific plugin instead of your theme’s `functions.php` to maintain functionality during theme switches.

## Conclusion

Custom Post Types are a fundamental feature for transforming WordPress from a simple blogging platform into a fully-fledged content management system. Whether you choose the precision of manual coding or the simplicity of a plugin, implementing CPTs empowers you to organize content logically, improve the user experience for content editors, and present information in a structured, professional manner on the front end. Start by planning your content structure, then use the method that best suits your skills to build a more powerful and tailored WordPress website.

Leave a Comment