CPT remove single view Wordpress frontend - php

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?

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

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/
├── style.css/
├── single-post.php/
└── index.php/

Related

The hierarchy of single pages with custom post types in WordPress

I have a custom post type named cities.
For this custom post type I will have N cities related.
I'm trying to create the files for theses pages and I thought this would work:
single.php - the generic one
single-cities.php - To list the cities
single-cities-{city-slug}.php - To bring the information for THE city
When I try:
/cities - It's showing the single.php content, not the single-cities content with the list.
/cities/sao-paulo - It's working. It shows the information about Sao Paulo.
Test:
If I delete the single-cities-{city-slug}.php, and try to access /cities/sao-paulo, it will bring the single-cities.php
What am I doing wrong in this hierarchy?
My code to register the custom post type:
$city_args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title'
),
'exclude_from_search' => false,
'menu_icon' => 'dashicons-building'
);
register_post_type('cities', $city_args);
Thanks!
UPDATE
I think I misunderstood the concept of a single page.
The single.php is just to render a single post, not to list the posts for a custom post type.
Reading more about this hierarchy, I'm trying to understand how my structure of listing cities and then bring the city information would fit.
Maybe I should create a page for cities, and then inside this page (page-cities.php), create the code to bring all the cities.
Clicking on each city, it should go to the single.php, is that it??
You'll want archive-cities.php to list all cities, single-cities for one post. Use this as a reference: https://codex.wordpress.org/Post_Type_Templates
The page listing all your posts is called an Archive, which lives at the slug declared in the register_post_type arguments.
/cities - This is your archive page, so it will call archive-cities.php
/cities/sao-paulo - This is a single post. It will first look for single-cities-{city-slug}.php, then for single-cities.php.
In most cases, you would only use single-cities-{city-slug}.php to make a unique template file for a specific city. Assuming every city post uses an identical layout, you only need single-cities.php.

how to create custom post type with templates

I am working on a project that requires a custom post type.
I am also required to have a breadcrumb that works with all types of post/page types.
The thing is that I haven't figured out a good way to achieve to have a custom post type that has a "startpage" and then some child pages.
in my world, i would like to set up a regular page (let's call it "cars") that I link to a template. The template should be somehow connected to the custom post type so when I create a post in custom post type (let's call it "BMW") and goes to that post, I want the website to be like http://test.com/cars/BMW.
The other problem here is that when I am on the page http://test.com/cars/bmw and what to run a function to see what my page ancestors the BMW page has, I get null because Wordpress recognise the BMW page as a post and cars as a page.
I don't know if you guys understand what I mean here but what is your best way to achieve a complete chain with custom post type so I can access or recognize all pages/post in the chain. Also, this is mostly for the breadcrumbs to work and I want that standard first-page "cars" in this chain so one can write content in the standard WYSIWYG that will appear on the site.
Thanks, everyone.
Updated
When you register the post type, you specify a slug which will be used for your URL base, provided has_archive is set to true. In this case you can use the archive-{slug}.php file to style the archive.
If you want to use a custom page instead, has_archive has to be false but then there's no reliable way to identify this "fake parent" from the single CPT post.
The best option is to introduce a configuration setting on a custom settings page the allows the user to select which page to use for the CPT's archive. In the breadcrumbs generation you'd then use this config value instead of hardcoding or URL string manipulation etc.
Your CPT:
register_post_type('cars', [
'labels' => [
'name' => 'Cars',
'singular_name' => 'Car'
],
'supports' => ['title', 'editor', 'author', 'thumbnail', 'comments', 'revisions'],
'taxonomies' => ['category', 'post_tag'],
'public' => true,
'exclude_from_search' => false,
'has_archive' => false,
'rewrite' => ['slug' => 'cars']
]);
After you change settings that affect rewrite rules, go to Dashboard > Settings > Permalinks page and click save to rebuild the cache.

Remove base slug in permalinks of hierarchical custom post type

I have a hierarchical Custom Post Type called project, registered as follows:
register_post_type( 'project',
array(
'public' => true,
'hierarchical' => true,
'rewrite' => array(
'with_front' => false
)
)
);
The URLs currently look like this:
https://example.com/project/hello
https://example.com/project/hello/world
I would like the URLs to look like this:
https://example.com/hello
https://example.com/hello/world
How can I most efficiently accomplish this such that other URLs are not interfered with (e.g. pages, blog posts, other custom post types)?
All the solutions that are currently out there either cause 404s on other pages on the site or do not work correctly with nested URLs.

Wordpress - Custom post type "all posts" listing taxonomies

I've created a custom post type with several taxonomies.
The thing is that's listing all taxonomies in the custom post type's listing page (All pages button) and I would like to choose which ones to display and which ones not.
Googled a lot, but can't find a way to solve this.
Anyone knows wich functions/flags are necesary to let wordpress know if I want X taxonomy to be shown in the listing page of the custom post it's related to?
Sorry if I don't add any code, I believe it's not necesary since it's a teorical question.
Thanks in advance!
Found it!
When declaring a taxonomy, these are the args you have to pass.
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => 'vehicle_fuel_type',
'rewrite' => $rewrite,
);
show_admin_column declares if the taxonomy should be shown on the post's listing page or not.
Hope someone finds it useful

Create new admin section in wordpress - for creating office-pages

Ok, I have been looking for hours and I have to say I am really lost. I am trying to create a new section in the admin section of wordpress that should enable the user to create a new custom "office" page.
There is really many office pages on the site I have been working on (over 30), each with its opening hours, map, and images. I assume the client will want to add more later (or remove them) and they would like to manage it through Wordpress. That would mean adding a section that would enable them to put in the name of the office, opening hours, images and the location and it would create a new office page. I am rather a front-end developer and I have never worked with Wordpress before. I understand the loop, etc, I have read several things about Themes and how to create them but I am seriously stuck with how to create a section in admin area that would enable page creation/deletion with certain options.
Any help is greatly appreciated, just please point me to the correct direction. Web pages, WP codex, tutorials, youtube... whatever that helps. Thanks a bunch!
You can make custom post type..
By writing Code in function.php
function function-name(){
register_post_type( 'post name',
array(
'labels' => array(
'name' => __( 'post name' ),
'singular_name' => __( 'post name' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'post name'),
'supports' => array('title','editor','author','thumbnail','comments','custom-fields'),
)
);
}
add_action('init', 'function-name');
3 steps : -
1. create office post type by using Register Post Type
2. Create office categories by using Register taxonomy
3. create a meta box by using Add Meta Box for extra fields which wordpress doesnt offer by default like (office hours )
hope it helps !

Categories