WordPress
WordPress Custom Post Type PHP Registration

WordPress Custom Post Type Registration

Complete code snippet for registering a custom post type in WordPress with all essential arguments and REST API support.

Register a custom post type in WordPress with full REST API support and all essential features.

Usage

Add this code to your theme’s functions.php or a custom plugin:

function register_custom_post_type() {
    $labels = [
        'name'                  => 'Projects',
        'singular_name'         => 'Project',
        'menu_name'             => 'Projects',
        'name_admin_bar'        => 'Project',
        'archives'              => 'Project Archives',
        'attributes'            => 'Project Attributes',
        'parent_item_colon'     => 'Parent Project:',
        'all_items'             => 'All Projects',
        'add_new_item'          => 'Add New Project',
        'add_new'               => 'Add New',
        'new_item'              => 'New Project',
        'edit_item'             => 'Edit Project',
        'update_item'           => 'Update Project',
        'view_item'             => 'View Project',
        'view_items'            => 'View Projects',
        'search_items'          => 'Search Project',
        'not_found'             => 'Not found',
        'not_found_in_trash'    => 'Not found in Trash',
    ];
    
    $args = [
        'label'                 => 'Project',
        'description'           => 'Custom post type for projects',
        'labels'                => $labels,
        'supports'              => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'],
        'taxonomies'            => ['category', 'post_tag'],
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-portfolio',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'  => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
        'show_in_rest'          => true,
        'rest_base'             => 'projects',
    ];
    
    register_post_type('project', $args);
}
add_action('init', 'register_custom_post_type', 0);

Features

  • Full admin interface support
  • REST API enabled
  • Archive page support
  • Custom menu icon
  • Supports standard post features
  • Searchable and queryable

Customization

Change 'project' to your desired post type slug and update labels accordingly.