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
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:https://developer.wordpress.org/reference/functions/register_post_type/#public
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:
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 a404.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.
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.