-1

I have created a custom plugin that is similar to Woocommerce but will only manage products that are loaded using the WP REST API in a Vue SPA.

I've registered two custom post types, one for products and another for the shop departments.

At the moment I want to update the plugin code and I want to be able to add a category or taxonomy to the products CPT.

To be more clear, if I have a product that will need to be listed as a fruit I want to be able to assign it to the right category and get it inside the other custom post type, in this case the shop departments CPT.

How I need to modify the code so I can have a slug like this https://mywebsite.com/products/fruit/product-name ?

    public function setup_post_type()
    {
        //
        register_post_type( 
            'products',
            [
                'label' => __('Products', ''),
                'description' => __('...', ''),
                'show_in_rest' => true,
                'rest_namespace' => $this->namespace,
                'public' => true,
                'supports' => [
                    'title',
                    'editor',
                    'revisions',
                    'thumbnail'
                ],
                'show_ui' => true,
                'show_in_menu' => true,
                'menu_icon' => 'dashicons-store',
                'register_meta_box_cb' => [$this, 'register_product_metabox']
            ]
        );

        //
        register_post_type(
            'shop-departments',
            [
                'label' => __('Shop departments', ''),
                'description' => __('...', ''),
                'show_in_rest' => true,
                'rest_namespace' => $this->namespace,
                'public' => true,
                'supports' => [
                    'title',
                    'revisions',
                    'thumbnail'
                ],
                'show_ui' => true,
                'show_in_menu' => true,
                'menu_icon' => 'dashicons-excerpt-view' 
            ]
        );
    }

Thank you for the help

4
  • I tried to edit your question to make it clearer what you wanted as it heavily implies you don't know how to let your CPTs use categories which isn't what you want, your actual question is only passingly mentioned in the sentence "I need to modify the code so I can have a slug like this" and never actually states what part you needed adding ( I'm assuming fruit? ) You should state clearly and directly what you want as early as possible, what you've done is called "burying the lede"
    – Tom J Nowell
    Nov 29, 2023 at 14:12
  • I've updated the question. What I need to do is to be able to have products CPT as a child of shop departments CPT so I can assign for example an orange to the fruit shop department
    – ICTDEV
    Nov 29, 2023 at 14:21
  • hmm that contradicts what I just said, are you saying you know how to assign a term in a taxonomy to a CPT but don't know how to put the slug in the permalink/URL? Or are you saying you have a CPT, and a taxonomy, but don't know how to connect them so that the UI shows up? Are you actually asking 2 questions at the same time? Changing the URL is a separate question that has already been asked on the site multiple times, and has an independent unique solution that is different from being able to assign posts to a term in a taxonomy
    – Tom J Nowell
    Nov 29, 2023 at 14:28
  • @TomJNowell I don't know how to assign to a CPT a taxonomy and I want to categorize the CPT using taxonomies. I've expressed myself in a wrong way
    – ICTDEV
    Nov 29, 2023 at 15:57

1 Answer 1

1

You would do it by assigning the taxonomies when registering the post type:

'taxonomies' => [ 'taxonomy1', 'taxonomy2', ..etc ],

You can also use register_taxonomy_for_object_type to do this if you want a function call. You still need to register the taxonomies though or this won't work.

Note that this won't change your CPT's permalinks, but that's a separate question that has been asked many times on the site already.

1
  • Ok, thank you for the help. I will read about taxonomies to better understand when they can be useful for CPT registration.
    – ICTDEV
    Nov 30, 2023 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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