How to restrict vendor to post in their categories of services

Hello everyone,
I have a question regarding HivePress and TaskHive. I’ve created categories for my freelancers (vendors) and separate categories for services (listing categories).
How can I make sure that a freelancer can only post listings in the category they belong to?

For example, there are three categories for both freelancers and services (IT, Digital Marketing, Creativity). I want freelancers in the IT category to be able to offer services only in IT, and never in Digital Marketing or Creativity.

thanks for your help

Hi @jab97139,

You can achieve this by using a custom code snippet that restricts the available listing categories based on the vendor (freelancer) category. Here’s a solution provided by the HivePress dev, Ihor, in another topic:

Add the following code snippet to your child theme’s functions.php file or use a plugin like Code Snippets:

add_filter(
	'hivepress/v1/models/listing',
	function( $model ) {
		if ( hivepress()->helper->is_rest() ) {
			return $model;
		}
		$vendor = \HivePress\Models\Vendor::query()->filter(
			[
				'status__in' => [ 'auto-draft', 'draft', 'publish' ],
				'user'       => get_current_user_id(),
			]
		)->get_first();
		if ( ! $vendor ) {
			return $model;
		}
		// Replace the numbers below with your actual vendor and listing category IDs
		if ( array_intersect( (array) $vendor->get_categories__id(), [ 1 ] ) ) { // IT
			$model['fields']['categories']['option_args']['include'] = [ 4 ]; // IT services
		} elseif ( array_intersect( (array) $vendor->get_categories__id(), [ 2 ] ) ) { // Digital Marketing
			$model['fields']['categories']['option_args']['include'] = [ 5 ]; // Digital Marketing services
		} elseif ( array_intersect( (array) $vendor->get_categories__id(), [ 3 ] ) ) { // Creativity
			$model['fields']['categories']['option_args']['include'] = [ 6 ]; // Creativity services
		}
		return $model;
	},
	1000
);

How to use:

  • Replace 1, 2, 3 with your actual vendor category IDs (IT, Digital Marketing, Creativity).
  • Replace 4, 5, 6 with your actual listing category IDs for services.

This will ensure that when a freelancer tries to add a listing, they will only see the service category that matches their own vendor category.

I hope this helps!

Cheers,
Chris :victory_hand:

2 Likes

Hi Chris,
Thanks

In which database table can I find the différents IDs, please?
ID for vendors categories
ID for listing categories

Hi @jab97139,

In WordPress Dashboard, navigate to the edit screen of the vendor/listing category you want to identify, then look at the number portion that’s displayed in the URL. That’s the ID you’re looking for!

I hope this helps!

Cheers,
Chris :victory_hand:

2 Likes

Thanks.