Adding multiple categories to vendor profile - my endeavor

Hello HivePress team,

I’ve been working on adding the possibility for vendors to select up to 5 categories (most vendors/ professionals are able to do multiple things from different domains) when editing their profile. So I worked with AI, and after some time, I managed to achieve a partially satisfiable result through the following code:

// Add multiple category selection to the vendor profile edit form
add_filter(
    'hivepress/v1/forms/vendor_update',
    function( $form ) {
        $categories = get_terms([
            'taxonomy' => 'hp_vendor_category',
            'hide_empty' => false,
        ]);
        $options = [];
        foreach ($categories as $cat) {
            $options[$cat->term_id] = $cat->name;
        }

        $values = [];
        $user_id = get_current_user_id();
        $vendor_id = false;
        if ($user_id) {
            $vendor_query = new WP_Query([
                'post_type' => 'hp_vendor',
                'post_status' => 'publish',
                'author' => $user_id,
                'posts_per_page' => 1
            ]);
            if ($vendor_query->have_posts()) {
                $vendor_id = $vendor_query->posts[0]->ID;
            }
            wp_reset_postdata();
        }
        if ( $vendor_id ) {
            $terms = wp_get_object_terms($vendor_id, 'hp_vendor_category');
            if(count($terms) > 0) {
                foreach($terms as $t) { $values[] = $t->term_id; }
            }
        }

        $form['fields']['hp_vendor_category'] = [
            'type' => 'select',
            'label' => 'Select trades',
            'options' => $options,
            'value' => $values,
            'multiple' => true,
            'required' => true,
            'max_values' => 5, // Just informative
            'placeholder' => 'maximum 5 trades',
            'description' => 'You can select up to 5 categories',
        ];
        return $form;
    }
);

add_action(
    'hivepress/v1/models/vendor/update',
    function($vendor_id, $vendor){
        if( isset($_POST['hp_vendor_category']) ){
            $cats = $_POST['hp_vendor_category'];
            if(is_array($cats)) {
                if(count($cats) > 5) {
                    $cats = array_slice($cats, 0, 5);
                }
                $cats = array_map('intval', $cats);
                wp_set_object_terms(
                    $vendor_id,
                    $cats,
                    'hp_vendor_category',
                    false
                );
            } else {
                wp_set_object_terms(
                    $vendor_id,
                    intval($cats),
                    'hp_vendor_category',
                    false
                );
            }
        }
    },
    10,
    2
);

The issues I face:

  • When registering a new vendor, the default/inherited category selector still only allows single selection, then our additional multi-select appears (users end up selecting the category twice).

  • After saving, if you revisit the settings, only one selected category is visible in our multi-select field, even though all categories are actually linked to the vendor in the backend.

  • In the WP admin/backend, editing vendor categories still shows a single-select field, so only one category can be managed visually—though the database can store multiple.

What I want to do

Instead of adding an additional multi-select field, I would like to target and convert the default “category” selector for vendors into a multiple-selection box (both front-end and backend), so the experience is seamless and only one field controls category assignment everywhere.

Is there a recommended, up-to-date way to achieve this?
If not, would you consider adding support for this use case in a future HivePress release (for example, by allowing the built-in vendor category field to accept multiple categories and show them properly when re-editing the profile)?

Thank you!

Hi,

Please try these code snippets, Create listing with multiple categories on front-end - #9 by andrii.

If you change listing to vendor, it should do the trick.

Hope this helps.

1 Like

Thank you for directing me to the right solution. I managed to have this issue solved both on frontend and backend with the following code (+ some customisation and a small limitation)▼

// Add multiple categories to vendor backend
add_filter(
	'hivepress/v1/meta_boxes/vendor_attributes',
	function( $form ) {
		if(isset($form['fields']['categories'])){
			$form['fields']['categories']['multiple'] = true;

		}
		return $form;
	},
	1000
);

// Add multiple categories to vendor frontend/settings
add_filter(
	'hivepress/v1/forms/vendor_submit',
	function( $form ) {
		if(isset($form['fields']['categories'])){
			$form['fields']['categories']['multiple'] = true;
			unset( $form['fields']['categories']['attributes']['data-multistep'] );
			$form['fields']['categories']['label'] = 'ALEGE MESERII'; // schimbă titlul
			$form['fields']['categories']['max_values'] = 5; // limita la maxim 5
			$form['fields']['categories']['placeholder'] = 'maxim 5 meserii'; // text în input
			$form['fields']['categories']['description'] = 'Poți selecta maxim 5 categorii';
		}
		return $form;
	},
	1000
);

Best regards,

Ionut

2 Likes

Hi @Tomitzu,

Glad to hear you got things working! :slight_smile:

Thanks for sharing the solution with the community, too. I’m sure others will find it useful!

Cheers,
Chris :victory_hand:

2 Likes

Thanks for sharing!
It means a lot to the community and can be very helpful for others.

1 Like