WP_User_Query

Home » Classes » WP_User_Query

As a WordPress developer, you may find yourself in need of retrieving user data from your site’s database. This is where the WP_User_Query class comes in handy. This class allows you to easily query your database for users based on various parameters, such as roles, meta data, and search criteria.

The WP_User_Query class extends the WP_Query class, which means it shares many of the same parameters and methods. However, there are some differences that are specifically tailored to user queries.

To use the WP_User_Query class, simply instantiate a new object with the parameters you want to search for and call the get_results() method to retrieve the user data.

Here’s an example usage code:

$args = array(
    'role' => 'author',
    'meta_key' => 'favorite_color',
    'meta_value' => 'blue',
);

$user_query = new WP_User_Query( $args );

$authors_with_blue_favorite_color = $user_query->get_results();

In this example, we’re searching for all users with the "author" role who have a favorite color of "blue" stored as user meta data.

Overall, the WP_User_Query class is a powerful tool for retrieving user data from your WordPress site’s database.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.