0

I'm using CPT UI and I've manage to create a working custom post type called "Cars"
I would like to have "Sub Custom Post Type" like "Honda" for example and models like "M1", "M2", "M3" related to "Honda".

So if I click on Honda it will display the models.
How can I achieve the same concept of POST+CATEGORY+SUBCATEOGRY for custom post type
(i.e Cars+Honda+M1)

3

WordPress doesn't support nested post types, though you can created hierarchical types similar to the Core "Page" type. However, what you are describing, including this sample "POST+CATEGORY+SUBCATEOGRY" is a custom taxonomy for your Car post type. The Codex has examples, but you would need something like this (roughly, I am sure this needs tweaked):

add_action( 'init', 'create_book_tax' );

function create_book_tax() {
    register_taxonomy(
        'make',
        'cars',
        array(
            'label' => __( 'Make' ),
            'rewrite' => array( 'slug' => 'make' ),
            'hierarchical' => true,
        )
    );
}

https://codex.wordpress.org/Function_Reference/register_taxonomy#Example

0

yeah as answered by @s_ha_dum wordpress doesn't support the nested post type you can achieve it by xreating the custom taxonomy and by using parent child relation you can acheive your scenario. I am explaning the code as below.

It might be helpful to you.

     add_action( 'init', 'create_post_type' );
        function create_post_type() {
register_taxonomy('car', 'cars', array(
            'hierarchical' => true,
            'labels' => array(
                'name' => _x( 'Car Category', 'taxonomy general name' ),
                'singular_name' => _x( 'Car -Category', 'Car Category' ),
                'search_items' =>  __( 'Search Car Categories' ),
                'all_items' => __( 'All Car Categories' ),
                'parent_item' => __( 'Parent Car Category' ),
                'parent_item_colon' => __( 'Parent Car Category:' ),
                'edit_item' => __( 'Edit Car Category' ),
                'update_item' => __( 'Update Car Category' ),
                'add_new_item' => __( 'Add New Car Category' ),
                'new_item_name' => __( 'New Car Category Name' ),
                'menu_name' => __( 'Car Categories' ),
            ),

            'rewrite' => array(
                'slug' => 'car', 
                'with_front' => false, 
                'hierarchical' => true 
            ),
        ));

    register_post_type( 'cars',
            array(
            'labels' => array(
            'name' => __( 'Cars' ),
            'singular_name' => __( 'Cars' ),
            'menu_name' => __( 'Cars' ),
            'name_admin_bar' => __( 'Cars' ),
            'all_items' => __( 'Group of Companies' ),
            'add_new' => __( 'Add New Car' ),
            'new_item' => __( 'Add New Car' ),
            'add_new_item' => __( 'Add New Car' ),
            'edit_item' => __( 'Edit Car' ),
            'view_item' => __( 'View Car' ),
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array("title", "editor", "thumbnail")
            )
            );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.