Back to Snippets

Register a Custom Post Type with REST API Support

PHP Custom Post Types April 6, 2026

Register a custom post type with full labels, REST API support, archive page, and featured image. Production-ready with proper i18n.

Snippet Stats

Lines 25
Characters 1,074
Read 1 min
php • 25 lines
/**
 * Register 'portfolio' custom post type.
 *
 * @return void
 */
function prefix_register_portfolio_cpt(): void {
    register_post_type( 'portfolio', array(
        'labels'       => array(
            'name'               => __( 'Portfolio', 'theme-domain' ),
            'singular_name'      => __( 'Project', 'theme-domain' ),
            'add_new_item'       => __( 'Add New Project', 'theme-domain' ),
            'edit_item'          => __( 'Edit Project', 'theme-domain' ),
            'all_items'          => __( 'All Projects', 'theme-domain' ),
            'search_items'       => __( 'Search Projects', 'theme-domain' ),
            'not_found'          => __( 'No projects found.', 'theme-domain' ),
        ),
        'public'       => true,
        'has_archive'  => true,
        'show_in_rest' => true,
        'rewrite'      => array( 'slug' => 'portfolio' ),
        'supports'     => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'menu_icon'    => 'dashicons-portfolio',
    ) );
}
add_action( 'init', 'prefix_register_portfolio_cpt' );

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
PHP
Register a hierarchical custom taxonomy with full labels, REST API support, and admin column. Attach it to any custom post type.

Register Custom Taxonomy with Hierarchical Terms