skip to Main Content

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?

3

Answers


  1. 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
    ] );
    

    https://developer.wordpress.org/reference/functions/register_post_type/#public

    Login or Signup to reply.
  2. 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/
    ├── style.css/
    ├── single-post.php/
    └── index.php/
    
    Login or Signup to reply.
  3. 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.

    Publicly Queryable

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search