Conditional search

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

2 years, 9 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #22216



    I am looking for two different searches according to user login. I have recruiter and Candidate in my user list. So, Recruiter can search for candidate and Candidate can search for a recruiter and HIde their own list from the search.

    I know I can create two different searches and exclude the user role. But I want to have the same search which can work with a conditional search on user login.

    You must be logged in to view attached files.


    Well, this might be possible via using a custom code, but I am not sure. I did a bit of research, and constructed the custom code below. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    This code will exclude the current users role + the admin user roles. So when testing, if you are logged in as admin, you might not see any results. Please note that there is no guarantee that this will work properly in any case.

    add_filter('asp_query_args', 'asp_exclude_roles_sp', 10, 1);
    function asp_exclude_roles_sp($args) {
      $user_roles = wcmo_get_current_user_roles();
      $args['user_search_exclude_roles'] = array_merge(
      return $args;
    function wcmo_get_current_user_roles() {
       if( is_user_logged_in() ) {
         $user = wp_get_current_user();
         $roles = ( array ) $user->roles;
         return $roles; // This returns an array
       } else {
        return array();
    Ernest Marcinko

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




    The code Didn’t work for me. I end up creating two different search pages and provide a condition on login.


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

You must be logged in to reply to this topic.