Custom Post Type with archive page but no detail page

I want to have custom post type that have only title and archive page, but no detail page.

Here is what custom post type code:

add_action( 'init', 'register_cpt_trivia' );

function register_cpt_trivia() {

    $labels = array( 
        'name' => _x( 'Trivia', 'trivia' ),
        'singular_name' => _x( 'Trivia', 'trivia' ),
        'add_new' => _x( 'Add New', 'trivia' ),
        'add_new_item' => _x( 'Add New Trivia', 'trivia' ),
        'edit_item' => _x( 'Edit Trivia', 'trivia' ),
        'new_item' => _x( 'New Trivia', 'trivia' ),
        'view_item' => _x( 'View Trivia', 'trivia' ),
        'search_items' => _x( 'Search Trivia', 'trivia' ),
        'not_found' => _x( 'No trivia found', 'trivia' ),
        'not_found_in_trash' => _x( 'No trivia found in Trash', 'trivia' ),
        'parent_item_colon' => _x( 'Parent Trivia:', 'trivia' ),
        'menu_name' => _x( 'Trivia', 'trivia' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Few line text with no detail pages.',
        'supports' => array( 'title' ),

        'public' => false,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 20,

        'show_in_nav_menus' => false,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => true,

        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'trivia', $args );
}

Related posts

Leave a Reply

1 comment

  1. You can’t just disable single pages, you can however make it appear as if they do not exist. I suggest

    1) ensure that there are no links on the site pointing to the single page

    2) Create a file “single-trivia.php” in your theme with the content as <?php include '404.php';?>

    3) use template_redirect or template_include hook to change the template to 404 if it’s a single trivia page

    4) use post_row_actions hook to remove the view link from admin panel