Hide Category based on user role/membership plan ID

Hi!

First of all, as always, thank you for the best listing plugin out there!

I read this post about hiding a certain category for specific membership-IDs when submitting a listing. Hide categories only for a specific plan - #10 by yevhen

I have two question:

  1. This is based on the membership ID which changes for every user that is granted a membership (on my site I do this manually). Is it possible to use the plan ID? Because the plan is always the same.

  2. Is it possible to do the same thing based on user role ID? This would help me alot.

With best regards,

bb

  1. The code snippet from the linked topic is based on the membersip plan ID

  2. Please try this PHP snippet. Please change 1,2,3 on the categories id that you want to remove from the selection and please change role name on the role name which user has to not see some categories

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('role name', $user->roles)){
			$disabled_categories_ids = [1,2,3];
			
			if(isset($fields['categories']['options'])){
				foreach($disabled_categories_ids as $category_id){
					unset($fields['categories']['options'][$category_id]);
				}	
			}
		}
		
		return $fields;
	},
	10000
);

Worked perfectly! Thank you so much. :slight_smile:

One more question, is it possible to remove the first selected null category which is “-” from the dropdown?

With best regards,

bb

Please try this PHP snippet.

add_filter(
	'hivepress/v1/models/listing/fields',
	function($fields){
		if(isset($fields['categories'])){
				$fields['categories']['placeholder'] = 'Custom text';
		}
		
		return $fields;
	},
	10000
);

Hi again and thanks for your response!

I think you misunderstood me. I want to remove the “no category” selection which is represented by a “-“, not rename it to something else.

With best regards,

bb

Unfortunately it’s not possible if the drop-down is optional, the placeholder is always there.

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