Register a custom post type in WordPress the right way

Register a custom post type in WordPress the right way

WordPress has two built-in content types, posts and pages. As soon as a site needs structured content of another kind (portfolio items, events, team members, properties), putting it into posts with custom fields gets messy: the blog list mixes articles with team profiles, the editor shows boxes that do not belong there, and the URLs say nothing about the content. A custom post type (CPT) gives such content its own admin menu, list screen, editor, archive URL and templates.

Problem: Posts and pages do not fit structured content like a portfolio, events or team members.

Solution: Register a custom post type with register_post_type() on the init hook and, if it needs its own classification, a taxonomy with register_taxonomy().

Register the post type

In functions.php or, better, in a small plugin (then the content does not disappear when the theme is changed):

<?php
add_action( 'init', 'ha_register_portfolio_cpt' );

function ha_register_portfolio_cpt() {
    $labels = array(
        'name'          => __( 'Portfolio', 'theme-name' ),
        'singular_name' => __( 'Portfolio Item', 'theme-name' ),
        'add_new_item'  => __( 'Add New Portfolio Item', 'theme-name' ),
        'edit_item'     => __( 'Edit Portfolio Item', 'theme-name' ),
        'view_item'     => __( 'View Portfolio Item', 'theme-name' ),
        'all_items'     => __( 'All Portfolio Items', 'theme-name' ),
        'search_items'  => __( 'Search Portfolio', 'theme-name' ),
        'not_found'     => __( 'No portfolio items found.', 'theme-name' ),
    );

    register_post_type( 'portfolio', array(
        'labels'        => $labels,
        'public'        => true,
        'has_archive'   => true,                     // archive at /portfolio/
        'rewrite'       => array( 'slug' => 'portfolio' ),
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest'  => true,                     // block editor and REST API
        'menu_icon'     => 'dashicons-portfolio',
        'menu_position' => 5,
    ) );
}

What the main arguments do:

  • public shows the post type in the admin and on the site, and includes it in search;
  • has_archive creates the archive page; a string instead of true sets its URL;
  • supports lists the editor features: title, editor, featured image, excerpt, custom fields, revisions…;
  • show_in_rest is required for the block editor: without it the post type opens in the classic editor.

The post type name (portfolio) is at most 20 characters, lowercase, without spaces. It is stored in the database with every post, so choose it once.

A taxonomy for it

<?php
add_action( 'init', 'ha_register_portfolio_category' );

function ha_register_portfolio_category() {
    register_taxonomy( 'portfolio_category', 'portfolio', array(
        'label'        => __( 'Portfolio Categories', 'theme-name' ),
        'hierarchical' => true,  // like categories; false = like tags
        'rewrite'      => array( 'slug' => 'portfolio-category' ),
        'show_in_rest' => true,
        'show_admin_column' => true,
    ) );
}

The posts of a term are then queried as described in querying posts by a custom taxonomy term.

Flush the permalinks

After adding the code, go to Settings → Permalinks and click Save Changes. Until the rewrite rules are rebuilt, the URLs of the new post type return 404. In a plugin, call flush_rewrite_rules() on activation instead, never on every init: it is a heavy operation.

NOTE: The templates are picked by the hierarchy: single-portfolio.php for a single item and archive-portfolio.php for the archive. If they do not exist, WordPress falls back to single.php and archive.php.