How to Create Custom Post Types in WordPress

How to Create Custom Post Types in WordPress

WordPress is one of the most popular content management systems (CMS) worldwide, used to build everything from personal blogs to complex e-commerce websites. What makes WordPress stand out is its versatility, offering a wide range of themes, templates, and functionalities, all while allowing extensive customization options for users.

One of the standout features of WordPress is its ability to create highly customized content types beyond just posts and pages. In this post, we’ll guide you through the process of creating a Custom Post Type (CPT) in WordPress, a powerful feature for structuring unique content types on your site.

Why Create a Custom Post Type?

Custom Post Types allow you to define new content types in WordPress, tailored to the needs of your website. Whether you’re building a portfolio, a product catalog, or any other specialized content, Custom Post Types provide the flexibility to organize and display content in unique ways.

If you’re interested in learning how to create custom fields in WordPress, you can check out our previous post here: How to Create Custom Fields in WordPress.

Two Ways to Create a Custom Post Type in WordPress

There are two primary methods for creating a Custom Post Type:

  1. Using a Plugin (e.g., CPT UI)
  2. Manually Writing the Code

Creating a Custom Post Type Using the CPT UI Plugin

The easiest way to create a Custom Post Type is by using a plugin like CPT UI (Custom Post Type UI).

Here’s how you can do it:

  1. Install the CPT UI Plugin
    Navigate to the Plugins section of your WordPress dashboard and search for “CPT UI”. Install and activate the plugin called Custom Post Type UI by WebDevStudios.
  2. Add a New Custom Post Type
    After activation, you’ll see a new menu item called CPT UI in the WordPress admin sidebar. Click on it, then select Add/Edit Post Types.
  3. Configure the Post Type
    In the form, you’ll need to fill in the following details:
    • Post Type Slug: This is the unique identifier for your custom post type. For example, if you create a slug called cpt_inspirations, the URL for this post type will look like:
      https://www.yourdomain.com/wp-admin/edit.php?post_type=cpt_inspirations
    • Labels: Define the labels that will be used for your post type in the WordPress admin interface (like “Add New”, “All Inspirations”, etc.).
    • Note: Ensure that your Post Type Slug is unique and doesn’t conflict with existing WordPress post slugs.
  4. Customize Additional Settings
    The plugin provides options to further customize your post type’s features, such as its visibility, hierarchical structure, and what features (like title, editor, or thumbnail) it will support.

Method 2: Manually Registering a Custom Post Type

If you want more control or prefer not to use a plugin, you can manually create a Custom Post Type by adding code to your theme’s functions.php file.

Here’s how you can register a custom post type with code:

  1. Add the Custom Post Type Registration Code
    In your theme’s functions.php file, add the following code snippet:
function register_cpt_inspirations() {
    $labels = [
        "name" => __("Inspirations", "your_theme"),
        "singular_name" => __("Inspiration", "your_theme"),
    ];

    $args = [
        "label" => __("Inspirations", "your_theme"),
        "labels" => $labels,
        "public" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "has_archive" => false,
        "rewrite" => ["slug" => "inspirations", "with_front" => true],
        "supports" => ["title", "editor", "thumbnail", "excerpt"],
    ];

    register_post_type("cpt_inspirations", $args);
}

add_action('init', 'register_cpt_inspirations');
  1. Customize the Code
    • Post Type Slug: The slug here ('inspirations') will be part of the URL for your Custom Post Type. For example:
      https://www.yourdomain.com/inspirations/your-post-slug
    • Supports: Customize what features your custom post type will support. In the example above, we’ve included title, editor, thumbnail, and excerpt.
  2. Activate the Custom Post Type
    After adding the code, your custom post type will be available in the WordPress admin area. You can start adding content and customizing it according to your needs.

Customizing Your Custom Post Type

Whether you’re using the CPT UI plugin or manually adding the code, WordPress offers a wealth of customization options for your Custom Post Type. You can fine-tune everything from the URL structure and visibility to what metadata it supports. This makes it an incredibly flexible tool for organizing and displaying content on your website.


Conclusion

Creating Custom Post Types in WordPress opens up a world of possibilities for content organization. Whether you choose to use a plugin like CPT UI or write custom code, both methods allow you to create highly tailored content structures for your site.

If you run into any issues while creating your custom post type, feel free to leave a comment below, and we’ll be happy to help!

Image Credit : CC0 licensed photo by Webizito Web from the WordPress Photo Directory.



1 thought on “How to Create Custom Post Types in WordPress”

Leave a Reply