Custom Post Type (CPT) Generator for WordPress

2025-10-06 17:03:22 0
Web Development

Create code to register a Custom Post Type in WordPress called [CPTNAME]


Here is the optimized code snippet to create a Custom Post Type named [TOPIC] with a singular slug, compatible with [LANGUAGE]:

function create_custom_post_type_[prompt]() {
    $labels = array(
        'name'                  => '[TOPIC]',
        'singular_name'         => '[TOPIC]',
        'menu_name'             => '[TOPIC]',
        'name_admin_bar'        => '[TOPIC]',
        'archives'              => '[TOPIC] Archives',
        'attributes'            => '[TOPIC] Attributes',
        'parent_item_colon'     => 'Parent [TOPIC]:',
        'all_items'             => 'All [TOPIC]',
        'add_new_item'          => 'Add New [TOPIC]',
        'add_new'               => 'Add New',
        'new_item'              => 'New [TOPIC]',
        'edit_item'             => 'Edit [TOPIC]',
        'update_item'           => 'Update [TOPIC]',
        'view_item'             => 'View [TOPIC]',
        'view_items'            => 'View [TOPIC]',
        'search_items'          => 'Search [TOPIC]',
        'not_found'             => 'No [TOPIC] found',
        'not_found_in_trash'    => 'No [TOPIC] found in Trash',
        'featured_image'        => 'Featured Image',
        'set_featured_image'    => 'Set featured image',
        'remove_featured_image' => 'Remove featured image',
        'use_featured_image'    => 'Use as featured image',
        'insert_into_item'      => 'Insert into [TOPIC]',
        'uploaded_to_this_item' => 'Uploaded to this [TOPIC]',
        'items_list'            => '[TOPIC] list',
        'items_list_navigation' => '[TOPIC] list navigation',
        'filter_items_list'     => 'Filter [TOPIC] list',
    );

    $args = array(
        'label'                 => '[TOPIC]',
        'description'           => 'Custom Post Type for [TOPIC]',
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'custom-fields', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-admin-page',
        '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'             => '[prompt]',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'slug'                  => '[prompt]'
    );

    register_post_type( '[prompt]', $args );
}
add_action( 'init', 'create_custom_post_type_[prompt]', 0 );

Remember to replace [prompt] with the actual name of the custom post type and [LANGUAGE] with the target language, if necessary. This code assumes that the target language is English, as no language-specific adjustments have been requested.

Instructions: Replace `[LANGUAGE]` with your target language (e.g., English) and `[TOPIC]` with your article subject (e.g., 'How to Start a Successful Blog').