If you’re trying to create a hierarchal structure with regards to custom post types you’ll need to register a custom taxonomy on a more abstract version of your desired post type.
For example instead of naming your custom post type cars
name it vehicle
. You could then a register a hierarchical taxonomy with register_taxonomy()
. In custom_post_type()
you’d call register_taxonomy()
as follows:
function custom_post_type() {
// ... update labels to be more 'vehicle' friendly
$args = [
// ...
'taxonomies' => [ 'vehicle_type' ] // name of the new taxonomy you'll be creating
// ...
];
register_post_type( 'vehicle', $args );
$taxonomy_labels = [
// ... entries here are very similar to labels for `register_post_type`
];
$taxonomy_args = [
'labels' => $taxonomy_labels,
'hierarchical' => TRUE // gives taxonomy a "category" like structure
];
register_taxonomy( 'vehicle_type', $taxonomy_args );
}
add_action( 'init', 'custom_post_type', 0 );
NOTE: The name passed into register_taxonomy()
MUST be the same as the entry in the taxonomies
array in the args
array for the registered post type()
call for vehicle
.
For more information on register_taxonomy
see here.
Hope this helps.