Skip to main content

How to Create and Manage Custom Post Types in WordPress

WordPress is a versatile platform that allows users to extend its functionality in various ways. One of the most powerful features is the ability to create Custom Post Types (CPTs). These allow you to organise and display different types of content beyond the default posts and pages, making WordPress more than just a blogging tool.

In this blog post, we’ll walk you through what custom post types are, why you might need them, and how to create and manage them effectively.

What is a Custom Post Type?

WordPress comes with several default post types, including:

  • Post (for blog content)
  • Page (for static content)
  • Attachment (for media files)
  • Revisions (for content versions)

However, if you’re working on a more complex site, such as a portfolio, real estate listings, or a movie database, you’ll need different types of content. This is where Custom Post Types come into play. You can create a specific post type for your needs, such as:

  • Products for an e-commerce store
  • Portfolio for showcasing work
  • Testimonials for reviews or feedback
  • Events for calendars

Why Use Custom Post Types?

Creating a custom post type offers multiple advantages:

  1. Content Organisation: It keeps content separate and easier to manage.
  2. Customisation: You can apply custom templates to display content in a way that suits your website’s structure and design.
  3. Flexibility: You can add custom taxonomies, fields, and meta boxes to further enhance the structure of your content.
  4. SEO Benefits: Custom post types help in optimising content for search engines, especially when dealing with structured content like products or events.

How to Create a Custom Post Type in WordPress

There are two main methods for creating custom post types:

  1. Using a Plugin
  2. Using Code (functions.php)

Method 1: Using a Plugin

If you’re not comfortable writing code, the easiest way to create a custom post type is by using a plugin like Custom Post Type UI.

Here’s how to do it:

  1. Install and activate the Custom Post Type UI plugin from the WordPress plugin repository.
  2. After activation, navigate to CPT UI > Add/Edit Post Types in the WordPress dashboard.
  3. Fill out the required fields:
    • Post Type Slug: This will be the URL-friendly name (e.g., “portfolio”).
    • Plural Label and Singular Label: Set the labels for your custom post type.
    • Configure additional settings like visibility, hierarchical structure, and menu position.
  4. Click Add Post Type.

You now have a custom post type! It will appear in your WordPress dashboard menu, and you can start adding content.

Method 2: Adding Custom Post Types Using Code

For developers or those comfortable with basic coding, adding a custom post type via the functions.php file or a custom plugin offers more control.

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

function create_portfolio_cpt() {
    $labels = array(
        'name'               => _x( 'Portfolios', 'Post Type General Name', 'textdomain' ),
        'singular_name'      => _x( 'Portfolio', 'Post Type Singular Name', 'textdomain' ),
        'menu_name'          => __( 'Portfolios', 'textdomain' ),
        'name_admin_bar'     => __( 'Portfolio', 'textdomain' ),
        'add_new_item'       => __( 'Add New Portfolio', 'textdomain' ),
        'edit_item'          => __( 'Edit Portfolio', 'textdomain' ),
        'new_item'           => __( 'New Portfolio', 'textdomain' ),
        'view_item'          => __( 'View Portfolio', 'textdomain' ),
    );

    $args = array(
        'label'              => __( 'Portfolio', 'textdomain' ),
        'description'        => __( 'A custom post type for portfolios', 'textdomain' ),
        'labels'             => $labels,
        'supports'           => array( 'title', 'editor', 'thumbnail' ),
        'public'             => true,
        'show_in_menu'       => true,
        'menu_position'      => 5,
        'has_archive'        => true,
        'rewrite'            => array( 'slug' => 'portfolios' ),
        'show_in_rest'       => true,
    );

    register_post_type( 'portfolio', $args );
}
add_action( 'init', 'create_portfolio_cpt' );

Once you add this code to your functions.php file (or create a site-specific plugin), you’ll see a new “Portfolios” section in your WordPress dashboard. You can further customise this by adding more arguments like taxonomies, custom fields, or REST API settings.

Adding Custom Taxonomies

Just like the default post types, custom post types can have their own categories and tags, or you can create custom taxonomies for them.

To add a custom taxonomy for your post type (e.g., “Portfolio Categories”), you can use the register_taxonomy function in the same functions.php file:

function create_portfolio_taxonomy() {
    $labels = array(
        'name'          => _x( 'Portfolio Categories', 'taxonomy general name', 'textdomain' ),
        'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name', 'textdomain' ),
    );

    $args = array(
        'labels'        => $labels,
        'public'        => true,
        'hierarchical'  => true,
        'show_in_rest'  => true,
    );

    register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args );
}
add_action( 'init', 'create_portfolio_taxonomy' );

This will add a custom taxonomy to categorise your portfolios.

Customising Display with Templates

To display custom post types on the front end, you may need to create custom templates. WordPress uses its template hierarchy to look for files named according to the post type, such as:

  • single-portfolio.php (for individual posts)
  • archive-portfolio.php (for archive pages)

You can copy the default single.php or archive.php files from your theme and rename them for your custom post type. Then, you can customise these files to suit the design and layout of your custom post type.

Managing Custom Post Types in the Dashboard

WordPress makes it easy to manage custom post types in the dashboard:

  • You can add, edit, and delete custom posts just like regular posts.
  • Use plugins like Advanced Custom Fields (ACF) to add custom fields to your custom post types, giving you even more flexibility over how data is entered and displayed.

Conclusion

Custom post types are a great way to extend WordPress’s capabilities, allowing you to create a more dynamic and organised site. Whether you’re using a plugin for simplicity or coding for more control, CPTs provide flexibility and structure for unique content types.

By following the steps outlined in this guide, you’ll be able to create and manage custom post types that are tailored to your site’s specific needs.

Get in Touch
Silk

Author Silk

More posts by Silk