GET FREE VERSION GET THE PRO VERSION

Users Search

Search and display user archive pages.

In this article
Following article describes features available only with PRO plugin version.

Overview

It is possible to search not only for products and terms archive pages but also for users' archive pages.

Users search results will be placed at the top of the search results list and will link to the user archive page with all owned by this user products. This link also can be customized via a plugin filter.

Users inside search results

Users inside search results

Here is a steps to enable users search feature:

1. First of all it is needed to enable user search. Open plugin settings page -> Results tab and find Archive pages option. Turn on Users option.

Turning on users archive pages search.

Turning on users archive pages search.

2. Then it is necessary to click on the gear icon near this option and choose what user roles must be available for search. By default all roles are excluded from the search. Choose what roles you need to enable.

All user roles that are available for searching.

All user roles that are available for searching.

3. Done! After activating Users archive pages search and choosing needed user roles plugin is ready to search and display users results.

Users archive pages results.

Users archive pages results.

How to customize users search results

It is possible to customize user archive pages results by using aws_search_users_results filter. Below described some examples of customizations that can be done.

Changing users default link

For example, let's customize users' archive page URL inside the search results block. We will change it to '/?user-id=' . $user_id.  

add_filter( 'aws_search_users_results', 'my_aws_search_users_results', 10, 3 );
function my_aws_search_users_results( $results, $roles, $search_query ) {
    if ( $results ) {
        foreach( $results as $user_id => $user ) {
            $results[$user_id][0]['link'] = '/?user-id=' . $user_id;
        }
    }
    return $results;
}

Changing users title

Let's add a user role along with the name inside the search results block.

Show user role inside search results block.

Show user role inside search results block.

add_filter( 'aws_search_users_results', 'my_aws_search_users_results', 10, 3 );
function my_aws_search_users_results( $results, $roles, $search_query ) {
    if ( $results ) {
        foreach( $results as $user_id => $user ) {
            $user_obj = get_user_by( 'id', $user_id );
            if ( $user_obj ) {
                $role = $user_obj->roles[0];
                $results[$user_id][0]['name'] .= ' ( ' . $role . ' )';
            }
        }
    }
    return $results;
}

Adding user description

Lets show inside search results list some user description if he adds some Biographical Info from his WordPress profile page.

Show user description inside search results block.

Show user description inside search results block.

add_filter( 'aws_search_users_results', 'my_aws_search_users_results', 10, 3 );
function my_aws_search_users_results( $results, $roles, $search_query ) {
    if ( $results ) {
        foreach( $results as $user_id => $user ) {
            $userdata = get_user_meta( $user_id );
            if ( $userdata && isset( $userdata['description'] ) ) {
                $results[$user_id][0]['excerpt'] = $userdata['description'];
            }
        }
    }
    return $results;
}

Adding user email and url

Another possible solution - adding user email and website URL inside search results output.

Show user email and url inside search results block.

Show user email and url inside search results block.

add_filter( 'aws_search_users_results', 'my_aws_search_users_results', 10, 3 );
function my_aws_search_users_results( $results, $roles, $search_query ) {
    if ( $results ) {
        foreach( $results as $user_id => $user ) {
            $excerpt = '';
            $user_info = get_userdata($user_id);
            if ( $user_info && $user_info->user_email ) {
                $excerpt .= 'Email - ' . $user_info->user_email;
            }
            if ( $user_info && $user_info->user_url ) {
                $excerpt .= '<br>Website - ' . $user_info->user_url;
            }
            $results[$user_id][0]['excerpt'] = $excerpt;
        }
    }
    return $results;
}