Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Including user roles when do user search
- This topic has 6 replies, 2 voices, and was last updated 5 years, 5 months ago by
Ernest Marcinko.
-
AuthorPosts
-
December 9, 2020 at 3:14 pm #30599
zpintar27
ParticipantHello,
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,
ZarkoDecember 9, 2020 at 4:16 pm #30602Ernest Marcinko
KeymasterHi 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.
December 9, 2020 at 8:25 pm #30606zpintar27
ParticipantOk, I’ll try with this filter, but maybe it will be wise to think about including this feature into plugin. 😉
December 15, 2020 at 1:18 pm #30696zpintar27
ParticipantI’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; }December 15, 2020 at 2:02 pm #30703Ernest Marcinko
KeymasterI think it might be the wrong operator. Try “LIKE” instead of the “IN”:
'operator' => 'LIKE',I’m 99% sure that is going to help.
December 15, 2020 at 2:35 pm #30706zpintar27
ParticipantYea! Now works!
Thanks for quick response.
Great plugin, worth that money! 🙂
December 15, 2020 at 2:37 pm #30707Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- You must be logged in to reply to this topic.