We can use WordPress back-end user list to manage the users on the website. Users get built-in custom profile fields such as first name, last name, website, description etc. In this tutorial, we will be discussing how to search and display WordPress users with specific profile field value.
Introduction to WordPress User Profile Fields
WordPress built-in profile fields allows you to capture basic details about a user. Most of the WordPress sites are used for blogs and hence those built-in fields are enough to capture author details. However, we need custom profile fields in many other types of sites built with WordPress. In such scenarios, we can use plugins like User Profiles Made Easy allows you to create advanced custom field and capture wide range of user data.
Once you capture advanced user details, you may want to filter/search the users by specific profile field value. Let’s take a look at some of the practical scenarios for search users by profile field value.
- Real Estate Website – In such websites, city and country is commonly used custom profile fields to capture the location of property seller. So we may want to list the users in a specific city or country.
- Job Board Website – In such websites, Job category is a commonly used custom profile field to capture the interests of the applicants. So you may want to list job applicants by job category.
- Freelance Developers Website – In such websites, offered services or hourly rate could be commonly used custom fields. So you may want to list developers with specific service or specific hourly rate.
We can find many such example real world scenarios for needing search by custom profile fields in WordPress. In the next section, we are going to learn how to query the database and list the users with specific custom profile field value.
Searching Users by Custom Profile Field Value
Before searching custom fields, we need to identify how custom fields are stored in database. Most plugins uses wp_usermeta table to store the data of users custom profile fields. However, there can be plugins which uses its own custom tables to store users custom profile fields. In this tutorial, we are considering the common and recommended way of storing user details.
Let’s assume we have a custom profile field called job_category and wants to search all the members within Web Developer category. Following code searches the job_category field for the value Web Developer.
'job_category', 'meta_value' => 'Web Developer' , 'fields' => 'all' ) ); $users = $user_query->get_results(); if (!empty($users)) { echo '
- ';
foreach ($users as $user){
echo '
- ' . $user->display_name . '
'; } echo '
'; } else { echo 'No users found'; } } ?>
Here I have used a shortcode which can be used anywhere in the site. We use WordPress WP_User_Query class to query the users by job_category field with value as Web Developer. This will return all the Web Developers in the site and display their display_name using the loop. If you want to search users by multiple custom field values, you can use following query.
array( 'relation' => 'OR', array( 'key' => 'job_category', 'value' => 'Web Developer', 'compare' => '=' ), array( 'key' => 'hourly_rate', 'value' => '20', 'compare' => '=' ) ) ); $user_query = new WP_User_Query( $args ); ?>
This is the most simplest way of searching and listing users based on custom profile field values. If you have the development knowledge, you can alter the above shortcode and make it dynamic with dynamic profile fields and dynamic values instead of static values.
In the next section, we are going to see how User Profiles Made Easy plugin provides custom profile field search features. It will be ideal for non-developers who wants to display user list based on custom profile field values.
List Users by Custom Field Value with User Profiles Made Easy
User Profiles Made Easy plugin provide a shortcode called [upme] to show a list of members anyhwre in the site. We can use some of its attributes to show list of users by custom profile field as shown in the following code..
[upme group=all users_per_page=10 group_meta='job_category' group_meta_value='Web Developer' ]
If you have UPME installed, you don’t need to worry about handling queries or loops or any kind of coding. Just place the shortcode with correct user profile field and value. Then you will get a beautiful user list with job_category as Web Developers as shown in the following screenshot.
This will allow you to display users list based on specific job category. What if you want visitors to select the job category and get the results? User Profiles Made Easy also offers this feature through its search filters. All you need to do is add the search shortcode with custom field filter. Then you can select the job category as shown in the following screenshot and list users for the selected job category profile field value.
User Profiles Made Easy is one of the plugins that offers this feature. We recommend it as we are developing this plugin. You are welcome to check its features and compare it with other similar user profile management plugins.
Summary
In this tutorial, we discussed the need for searching users by custom profile field value. Then we created a query and displayed the searched users by custom field value. Finally, we looked at how User Profiles Made Easy plugin simplifies this feature with more advanced options.
Let us know your suggestions/issues through comments section.