WP User Query()
Naar navigatie springen
Naar zoeken springen
WP_User_Query
() is a WordPress PHP class for interacting with user info from a WordPress database.
Introduction
The WP_User_Query
is a class in WordPress that is used for querying and retrieving user information from the WordPress database. This class represents something like a query object. It does not represent a user.
It allows you to query and filter users based on various criteria, such as user roles, meta data, and more:
- Querying Users: You can use
WP_User_Query
to create custom queries to retrieve specific user data. This can be useful for various purposes, such as building member directories, retrieving user profiles, or performing actions related to specific users - Parameters: When using
WP_User_Query
, you can specify a wide range of parameters to filter the results, including user roles, meta data, search keywords, and more. These parameters allow you to tailor the query to your specific needs - Retrieving User Objects:
WP_User_Query
returns an array of WP_User objects that represent individual users. Each WP_User object contains user-related information, such as the user's ID, username, email, and other user meta data - Role-Based Queries: You can use the role parameter to filter users by their roles, allowing you to retrieve users with specific roles, such as authors, editors, or subscribers
- Custom User Meta: If you have custom user meta fields, you can use the meta_key and meta_value parameters to filter users based on custom user meta data
Note:
- The
--who=me
option in WP-CLI appears to be a simplified way of targeting the currently logged-in user when usingWP_User_Query
in a command-line context - The specific options and parameters available in WP-CLI may not always align perfectly with what's available in the underlying PHP functions.
Examples
Instantiate a WP_User_Query
object to get a list of all users:
# Create a new WP_User_Query instance with no specific query parameters # $user_query = new WP_User_Query(); # Get the list of users # $users = $user_query->get_results(); # Loop through the users and display their usernames # if (!empty($users)) { foreach ($users as $user) { echo 'Username: ' . $user->user_login . '<br>'; } } else { echo 'No users found.'; }