How to Implement Custom Post Types in WordPress

Posts and Pages are WordPress's built-in content types. But what about a portfolio of projects? A team member directory? A list of testimonials? A recipe collection? These are all content types that don't fit neatly into Posts or Pages — and WordPress gives you the tools to create exactly the right content type for any data.

Custom Post Types (CPTs) let you register entirely new content types with their own admin menu items, capabilities, labels, URL structures, REST API endpoints, and support for specific editor features.

This guide covers everything you need to know about implementing custom post types professionally — from basic registration to advanced customisation.

What Is a Custom Post Type?

In WordPress, everything stored in the wp_posts table is a "post" in the database sense — including Pages, Attachments (media), Navigation Menus, and Revisions. These are all technically post types, just different ones.

A Custom Post Type is simply a new entry in this family — registered by your theme or plugin to represent a specific type of content with its own:

  • Admin menu and submenu items
  • Custom labels ("Portfolio Items", "Add New Portfolio Item")
  • URL slug (/portfolio/project-name/)
  • REST API endpoints (/wp-json/wp/v2/portfolio)
  • Supported editor features (title, editor, thumbnail, etc.)
  • Taxonomies (categories and tags specific to this post type)
01
Registering a Custom Post Type

Always register custom post types from a plugin, not a theme. A CPT registered in a theme disappears (along with all its content) when the theme is switched.

PHP
add_action( 'init', 'register_portfolio_post_type' );

function register_portfolio_post_type() {
    $labels = array(
        'name'                  => 'Portfolio',
        'singular_name'         => 'Portfolio Item',
        'menu_name'             => 'Portfolio',
        'add_new'               => 'Add New',
        'add_new_item'          => 'Add New Portfolio Item',
        'new_item'              => 'New Portfolio Item',
        'edit_item'             => 'Edit Portfolio Item',
        'view_item'             => 'View Portfolio Item',
        'all_items'             => 'All Portfolio Items',
        'search_items'          => 'Search Portfolio',
        'not_found'             => 'No portfolio items found.',
        'not_found_in_trash'    => 'No portfolio items found in Trash.',
    );

    $args = array(
        'labels'              => $labels,
        'public'              => true,       // Publicly accessible
        'publicly_queryable'  => true,       // Can be queried via URL
        'show_ui'             => true,       // Show in admin
        'show_in_menu'        => true,       // Show in admin sidebar
        'query_var'           => true,       // Enable ?portfolio=slug queries
        'rewrite'             => array(
            'slug' => 'portfolio',           // URL prefix: /portfolio/item-slug/
        ),
        'capability_type'     => 'post',     // Use standard post capabilities
        'has_archive'         => true,       // Enable /portfolio/ archive page
        'hierarchical'        => false,      // Like posts (not pages)
        'menu_position'       => 20,         // Position in admin sidebar
        'menu_icon'           => 'dashicons-portfolio',  // Admin menu icon
        'supports'            => array(
            'title',           // Title field
            'editor',          // Content editor
            'thumbnail',       // Featured image
            'excerpt',         // Excerpt field
            'custom-fields',   // Custom fields panel
            'revisions',       // Revision history
            'page-attributes', // Order and parent (useful for hierarchical CPTs)
        ),
        'show_in_rest'        => true,       // Enable REST API and block editor
        'rest_base'           => 'portfolio', // REST API endpoint: /wp-json/wp/v2/portfolio
    );

    register_post_type( 'portfolio', $args );
}

Flush rewrite rules after registration:

After registering a new CPT, the URL rewrite rules need to be flushed. Do this automatically on plugin activation:

PHP — Activation Hook
register_activation_hook( __FILE__, function() {
    register_portfolio_post_type(); // Register before flushing
    flush_rewrite_rules();
});

register_deactivation_hook( __FILE__, function() {
    flush_rewrite_rules();
});
Manual flush: If you don't use activation hooks, go to Settings → Permalinks → Save Changes — this flushes rewrite rules without changing anything.
02
Registering a Custom Taxonomy

Taxonomies are the classification system for post types — similar to Categories and Tags but specific to your custom post type.

PHP
add_action( 'init', 'register_portfolio_taxonomy' );

function register_portfolio_taxonomy() {
    $labels = array(
        'name'              => 'Portfolio Categories',
        'singular_name'     => 'Portfolio Category',
        'search_items'      => 'Search Categories',
        'all_items'         => 'All Categories',
        'parent_item'       => 'Parent Category',
        'parent_item_colon' => 'Parent Category:',
        'edit_item'         => 'Edit Category',
        'update_item'       => 'Update Category',
        'add_new_item'      => 'Add New Category',
        'new_item_name'     => 'New Category Name',
        'menu_name'         => 'Categories',
    );

    register_taxonomy(
        'portfolio_category',        // Taxonomy name
        'portfolio',                 // Post type(s) to attach to
        array(
            'labels'            => $labels,
            'hierarchical'      => true,     // true = like categories, false = like tags
            'public'            => true,
            'show_ui'           => true,
            'show_admin_column' => true,     // Show in post list admin column
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'portfolio-category' ),
            'show_in_rest'      => true,     // Enable REST API and block editor
        )
    );
}
Tip: Set hierarchical to true for category-style behaviour (parent/child) or false for tag-style behaviour (flat).
03
Adding Custom Fields with ACF

Custom Post Types often need data beyond title and content — portfolio items need client name, project URL, technologies used; team members need job title, department, LinkedIn URL.

Advanced Custom Fields (ACF) is the standard solution. The free version handles most use cases; ACF Pro adds repeater fields, flexible content, and relationship fields.

Code-Based ACF Field Groups (Recommended for Developers)

Register field groups in PHP rather than through the UI — keeps configuration in version control:

PHP — Register ACF Fields
add_action( 'acf/init', 'register_portfolio_fields' );

function register_portfolio_fields() {
    if ( ! function_exists( 'acf_add_local_field_group' ) ) {
        return; // ACF not active
    }

    acf_add_local_field_group( array(
        'key'      => 'group_portfolio_details',
        'title'    => 'Portfolio Details',
        'fields'   => array(
            array(
                'key'   => 'field_client_name',
                'label' => 'Client Name',
                'name'  => 'client_name',
                'type'  => 'text',
            ),
            array(
                'key'        => 'field_project_url',
                'label'      => 'Project URL',
                'name'       => 'project_url',
                'type'       => 'url',
            ),
            array(
                'key'        => 'field_technologies',
                'label'      => 'Technologies Used',
                'name'       => 'technologies',
                'type'       => 'textarea',
                'rows'       => 3,
            ),
            array(
                'key'           => 'field_featured',
                'label'         => 'Featured Project',
                'name'          => 'is_featured',
                'type'          => 'true_false',
                'default_value' => 0,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param'    => 'post_type',
                    'operator' => '==',
                    'value'    => 'portfolio',
                ),
            ),
        ),
    ) );
}

Displaying ACF Fields in Templates

PHP — Template Usage
$client_name  = get_field( 'client_name' );
 $project_url  = get_field( 'project_url' );
 $technologies = get_field( 'technologies' );
 $is_featured  = get_field( 'is_featured' );
04
Custom Admin Columns

The default admin list for your CPT shows only title and date. Add meaningful columns for better usability:

PHP — Admin Columns
// Add columns to Portfolio admin list
add_filter( 'manage_portfolio_posts_columns', 'portfolio_admin_columns' );

function portfolio_admin_columns( $columns ) {
    $new_columns = array();

    // Insert after the checkbox
    foreach ( $columns as $key => $value ) {
        $new_columns[ $key ] = $value;
        if ( $key === 'title' ) {
            $new_columns['client_name'] = 'Client';
            $new_columns['is_featured'] = 'Featured';
        }
    }

    return $new_columns;
}

// Populate the columns
add_action( 'manage_portfolio_posts_custom_column', 'portfolio_admin_column_content', 10, 2 );

function portfolio_admin_column_content( $column, $post_id ) {
    switch ( $column ) {
        case 'client_name':
            $client = get_post_meta( $post_id, 'client_name', true );
            echo esc_html( $client ?: '—' );
            break;

        case 'is_featured':
            $featured = get_post_meta( $post_id, 'is_featured', true );
            echo $featured ? '⭐ Featured' : '—';
            break;
    }
}

// Make columns sortable
add_filter( 'manage_edit-portfolio_sortable_columns', 'portfolio_sortable_columns' );

function portfolio_sortable_columns( $columns ) {
    $columns['client_name'] = 'client_name';
    return $columns;
}
05
Custom Post Type in the REST API

Setting 'show_in_rest' => true in the CPT registration automatically creates a REST API endpoint. You can further customise the REST API integration — for example, adding ACF fields to the response:

PHP — REST API ACF Integration
// Add ACF fields to the REST API response
add_action( 'rest_api_init', function() {
    register_rest_field(
        'portfolio',
        'acf_fields',
        array(
            'get_callback' => function( $post ) {
                if ( function_exists( 'get_fields' ) ) {
                    return get_fields( $post['id'] );
                }
                return null;
            },
        )
    );
});

Now GET /wp-json/wp/v2/portfolio returns portfolio items with ACF field data included.

Headless WordPress: If you're building a headless setup (Next.js, React), exposing ACF fields via the REST API is essential for fetching complete CPT data in your frontend.
06
Querying Custom Post Types

Basic WP_Query

PHP — WP_Query
$portfolio_query = new WP_Query( array(
    'post_type'      => 'portfolio',
    'posts_per_page' => 12,
    'orderby'        => 'date',
    'order'          => 'DESC',
    'tax_query'      => array(
        array(
            'taxonomy' => 'portfolio_category',
            'field'    => 'slug',
            'terms'    => 'web-design',
        ),
    ),
) );

if ( $portfolio_query->have_posts() ) {
    while ( $portfolio_query->have_posts() ) {
        $portfolio_query->the_post();
        $client = get_post_meta( get_the_ID(), 'client_name', true );
        // Output each portfolio item
    }
    wp_reset_postdata(); // Reset after custom query
}

Meta Query (Filter by Custom Field)

PHP — Meta Query
// Get only featured portfolio items
 $featured_portfolio = new WP_Query( array(
    'post_type'      => 'portfolio',
    'posts_per_page' => 6,
    'meta_query'     => array(
        array(
            'key'     => 'is_featured',
            'value'   => '1',
            'compare' => '=',
        ),
    ),
) );

get_posts() (Simpler Alternative)

PHP — get_posts()
$items = get_posts( array(
    'post_type'   => 'portfolio',
    'numberposts' => 6,
    'orderby'     => 'title',
    'order'       => 'ASC',
) );

foreach ( $items as $item ) {
    echo '<h3>' . esc_html( $item->post_title ) . '</h3>';
}

Using a Plugin for CPT Registration

For non-developer users, or for a UI-based CPT management approach, Custom Post Type UI (CPT UI) plugin provides a visual interface for registering CPTs and taxonomies without code.

Useful for rapid prototyping or client handover — but production sites typically migrate to code-based registration for maintainability and version control.

Key Registration Arguments Reference

Argument Type Purpose Common Value
publicBooleanWhether publicly accessibletrue
show_in_restBooleanEnable REST API and block editortrue
has_archiveBooleanEnable archive page at /slug/true
hierarchicalBooleanLike pages (true) or posts (false)false
rewrite.slugStringURL prefix for the post type'portfolio'
capability_typeStringPermissions system to use'post'
menu_positionIntegerPosition in admin sidebar20
menu_iconStringDashicon class for admin menu'dashicons-portfolio'
supportsArrayEnabled editor featuresSee below
show_admin_columnBooleanShow taxonomy in admin list (taxonomy arg)true

The supports Array Options

Support Enables When to Include
'title'Title fieldAlmost always
'editor'Content editor (Gutenberg)Most content types
'thumbnail'Featured imageWhen images are needed
'excerpt'Excerpt fieldFor list/archive displays
'custom-fields'Custom fields meta boxIf not using ACF exclusively
'revisions'Revision historyFor important content
'page-attributes'Page parent/orderHierarchical CPTs only
'author'Author selectorMulti-author sites
'trackbacks'Pingbacks/trackbacksRarely needed

Hierarchical vs Non-Hierarchical CPTs

Aspect Non-Hierarchical (false) Hierarchical (true)
LikePostsPages
Parent/ChildNoYes
Taxonomy styleTags (flat)Categories (nested)
URL structure/slug/post-slug//slug/parent-slug/post-slug/
Use forPortfolio, Recipes, TestimonialsDocumentation, Services, Knowledge Base

Frequently Asked Questions

Q: Should I register CPTs in a theme or a plugin?
A: Always in a plugin. If a theme registers a CPT and the theme is changed, the CPT disappears from the admin — but the data remains in the database, orphaned and inaccessible. A dedicated simple plugin for your CPTs ensures they're always available regardless of theme.
Q: Do I need to flush rewrite rules every time I change CPT settings?
A: Only when the rewrite argument changes (the URL slug) or when the has_archive setting changes. For other changes (labels, capabilities, supports), no flush is needed. On production, do this by visiting Settings → Permalinks.
Q: How do I get my CPT to show in the block editor's post selector?
A: Ensure show_in_rest is set to true in your CPT registration arguments. The block editor uses the REST API — CPTs not in the REST API aren't available in the block editor's query blocks.
Q: Can I attach standard categories and tags to my CPT?
A: Yes — register the category and post_tag taxonomies when registering your CPT by adding them to the taxonomies argument: 'taxonomies' => array( 'category', 'post_tag' ). However, creating custom taxonomies specific to your CPT is generally better practice for clean data separation.
Q: What happens to CPT data if I delete the registration code?
A: The posts remain in the wp_posts database table — they just revert to showing as regular "posts" in the admin. This is why registering CPTs in plugins is critical: a theme switch doesn't delete your plugin, so your CPTs stay registered. Always back up before modifying CPT registration.
Q: Is there a limit to how many CPTs I can register?
A: No hard limit in WordPress. Practically, each CPT adds admin UI entries, rewrite rules, and REST API endpoints. For sites with many CPTs (10+), ensure your hosting has adequate PHP memory and that you're not registering unused CPTs. Keep CPTs focused on genuinely distinct content types.

Final Thoughts

Custom Post Types are one of WordPress's most powerful content architecture features. They let you model any kind of content — with appropriate labels, URL structures, taxonomies, custom fields, and admin interfaces — in a way that makes content management intuitive for editors while maintaining clean data architecture for developers.

Register CPTs in plugins, use ACF for custom fields, customise admin columns for usability, and expose CPTs in the REST API for headless or API-based workflows.

Need custom post types built for your WordPress project? Our development team creates clean, well-architected CPTs with proper taxonomies, custom fields, and admin customisation.

Contact us today and we'll sort it out.
Views: 174