Including user roles when do user search

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Including user roles when do user search

This topic contains 6 replies, has 2 voices, and was last updated by

 
Keymaster
1 year, 1 month ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #30599

    Participant

    Hello,

    Simple question:

    In the User Search tab of General option we have a box for choosing user roles to exclude from search. But it isn’t good enough for us. We need something for including user roles in search. Why? Because, if we have some user in two roles and one of them excluding user and one including, the user will be excluded what is wrong in our case. So, we need some box (or choosing option for include/exclude from user roles)

    Any suggestion?

    regards,
    Zarko

    #30602

    Hi Zarko,

    Well, there is no option for that, but you can try using the asp_query_args filter to maybe use a programmatical solution to filter by user meta.

    Try something like this:

    add_filter("asp_query_args", "asp_query_args_change", 10, 2);
    function asp_query_args_change($args, $search_id) {
    	// Roles list
    	$roles = array('role1', 'role2');
    
    	global $wpdb;
    	$args['user_meta_filter'][] = array(
    		'key'     => $wpdb->base_prefix."capabilities',
    		'value'   => roles,
    		'operator' => 'IN',
    		'allow_missing' => false
    	);
    
      return $args;
    }

    Add the roles to the $roles array variable. I could not test this code, so please be careful, read the safe coding guide.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #30606

    Participant

    Ok, I’ll try with this filter, but maybe it will be wise to think about including this feature into plugin. 😉

    #30696

    Participant

    I’ve just tried your code, but doesn’t work!
    When it’s applied no user is searched out. Are you sure that’s code is OK? (because I found some typos)

    add_filter("asp_query_args", "asp_query_args_change", 10, 2);
    function asp_query_args_change($args, $search_id) {
    	if ($search_id == 2) {
    	
    	// Roles list
    	$roles = array('um_clanovi-udruge');
    
    	global $wpdb;
    	$args['user_meta_filter'][] = array(
    		'key'     => $wpdb->base_prefix."capabilities",
    		'value'   => $roles,
    		'operator' => 'IN',
    		'allow_missing' => false
    	);
    	}
      return $args;
    }
    #30703

    I think it might be the wrong operator. Try “LIKE” instead of the “IN”:

    'operator' => 'LIKE',

    I’m 99% sure that is going to help.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #30706

    Participant

    Yea! Now works!

    Thanks for quick response.

    Great plugin, worth that money! 🙂

    #30707
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.