Search and display user archive pages.
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.
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.
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.
3. Done! After activating Users archive pages search and choosing needed user roles plugin is ready to search and display users 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.
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; }
Let's add a user role along with the name inside the 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; }
Lets show inside search results list some user description if he adds some Biographical Info from his WordPress profile page.
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; }
Another possible solution - adding user email
and website URL
inside search results output.
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; }