Possible to set the search category when search is being fired based on user role?

Hi!

I’ve been experimenting a little with hivepress/v1/forms/listing_search but haven’t been able to figure it out. The use case is that I don’t want to display the search categories. I have two different roles. Role “Alice” should only be able to search category id 40. Logged out users and Role “Bob” should only be able to search for category 60.

I’ve been playing around with this code:

add_filter(
	'hivepress/v1/forms/listing_search',
	function( $form ) {
		$categories = [60];
		
		foreach($categories as $category){
			unset($form['fields']['_category']['options'][$category]);
		}
		
		return $form;
	},
	1000
);

and this code:

add_filter(
	'hivepress/v1/models/listing/fields',
	function($fields){
		if(!is_user_logged_in()){
			return $fields;
		}
		
		// Get user.
		$user = get_userdata(get_current_user_id());
		
		if(in_array('contributor', $user->roles)){
			$disabled_categories_ids = [63];
			
			if(isset($fields['categories']['options'])){
				foreach($disabled_categories_ids as $category_id){
					unset($fields['categories']['options'][$category_id]);
				}	
			}
		}
			if(in_array('subscriber', $user->roles)){
			$disabled_categories_ids = [62];
			
			if(isset($fields['categories']['options'])){
				foreach($disabled_categories_ids as $category_id){
					unset($fields['categories']['options'][$category_id]);
				}	
			}
		}
		return $fields;
	},
	10000
);

but with no luck. When I read a little bit more I found this code snippet that perhaps takes me closer to a solution? However, this is for an attribute. I want to change the category.

add_action(
	'hivepress/v1/models/listing/search',
	function($query,$fields) {
		$meta_query = array_filter( (array) $query->get( 'meta_query' ) );
		$meta_query['pets'] = [
			'key' => 'hp_pets',
			'type' => 'CHAR',
			'compare' => 'EXISTS',
		];
		$query->set( 'meta_query', $meta_query );
	},
	1000,
	2
);

Can you please help me?

Thank you for as always a very fast and good support and for the best listing plugin out there.

With best regards,

bb

Sorry for the delay.

Yes, it’s possible but requires customizations. If you mean hiding specific categories from the drop-down then hivepress/v1/forms/listing_search hook can be used. You can get the current user there (via wp_get_current_user()) and check the role, then unset categories (the code in the first snippet seems to be ok, it should unset options).

Please note that this will work only if you enabled the category drop-down in the main search form via Default Fields, otherwise the category filter appears in the listing_filter sidebar form.

Hi and thanks for your answer!
Actually, I don’t want to display the category drop down in the search field so in Hivepress → Settings → (Search) Default Fields (optional) I have removed the Categories.

The use case is a job board. I want employers to be able to create jobs and jobseekers to be able to create CVs.

I have two categories in hivepress: 1. CV 2. jobs

Only employers (user roles employer and premiumemployer) have access to the page findemployees.php. I want employers to be able to search for CVs there.

Not logged in users, logged in jobseekers (user roles worker and premiumworker) and employers (user roles employer and premiumemployer) have access to findjobs.php. On this page I only want to display jobs and not CVs.

Can you help me solve this problem? I don’t know what to ask for to be able to do this. Maybe you have a solution for me? I thought of maybe catching the search query and if you are on the page findjobs.php only search for the category jobs. The same with the findemployees.php, if the user role is employer or premium employer catch the query and display only CVs from the category CV.

How can I solve this? Maybe you want to create some built in function to be able to do this? Maybe the blocks for the search field where you can select which category or attributes to search would be a good idea?

thank you for the best directory plugin out there!

With best regards,

bb

Sorry, there’s no simple code snippet for this, this would require a custom implementation. I can provide some general guidance if this helps, e.g. about the related hooks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.