Cpt Remove Single View Wordpress Frontend

I added a custom post type 'Catalog'. This CPT makes it posible to create A list of items like a catalog. For posts Wordpress has always a single view page on the frontend and I want remove these and show an 404 error if somebody tried visit the url.

register_post_type( 'catalog',
    array(
        'labels' => array(
            'name'          => 'Catalogus',
            'add_new'       => 'Nieuw item',
            'add_new_item'  => 'Nieuw item toevoegen',
            'new_item'      => 'Nieuw item',
        ),
        'public' => false,
        'show_in_rest' => false,
        'menu_icon' => 'dashicons-store',
        'menu_position' => 2
    )
);

I set the public on false but this removes the whole CPT on the front and adminside.

Who can help me?

1

4 Answers

When you register a custom post type, a number of other values will set their defaults based on the value used for public. By setting public to false, the argument to show the UI in the admin panel will also have defaulted to false.

Pass in show_ui to control whether it's displayed in the admin panel:

register_post_type( 'catalog', [
    'labels' => [
        // labels...
    ]
    'public' => false,
    'show_in_rest' => false, // default is false so probably not needed
    'show_ui' => true, // show the admin UI for the CPT even when public is false
    'menu_icon' => 'dashicons-store',
    'menu_position' => 2
] );

For anyone in the community that's using the Custom Post Type's UI (CPT UI) plugin, just set "Publicly Queryable" to false. This will disable the single page for your CPT.

Any (custom) post types is ruled by the Wordpress Template Hierarchy.

As you can see here with the Visual Overview, any post type will be displayed by the following templates:

Custom Post Type

$custom.php → (fallback) single-$posttype-$slug.php → (fallback) single-$posttype.php → (fallback) single.php → (fallback) singular.php

Blog Post

$custom.php → (fallback) single.php → (fallback) singular.php

One way to remove the single custom post type display ability and keeping the query ability is to remove and add the adequate templates.

Following best practices, by removing single.php & singular.php and not including any custom templates, you will effectively remove a custom post type ability to produce a single template which will redirect the request to a 404.php and corresponding fallbacks.

To make sure our blog posts still renders and have a single post type display ability, we need to include a single-post.php which will only display theme's blog posts.

Here is a minimal graphical representation of a theme's folder with no single custom post type ability and a single blog post post ability.

myTheme/
├── 
├── 
└── 

To prevent WordPress from creating a view page for your custom post type (CPT), you can use the publicly_queryable parameter when registering the CPT. By setting publicly_queryable to false, you can disable the view page for the CPT. Here's an example:

function register_custom_post_type() {
    $args = array(
        'labels' => array(
            'name' => 'Your Custom Post Type',
            'singular_name' => 'Custom Post',
        ),
        'public' => true,
        'publicly_queryable' => false,  // Disable view page
        // Rest of the CPT arguments
    );

    register_post_type('your_custom_post_type', $args);
}
add_action('init', 'register_custom_post_type');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest