This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Conditional search

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

    Hi,

    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.

    #22229
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    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(
        array('administrator'),
        $user_roles
      );
      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();
       }
    }
    #22255
    unnatiunnati
    Participant

    Hi,

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

    Thanks,
    Unnati

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.