WP_User_Query is a class that allows querying WordPress database tables ‘wp_users’ and ‘wp_usermeta’. The was class introduced in Version 3.1 and it deprecated the old WP_User_Search class. It is a convenience class which functions almost exactly the same as WP_Query only for users.
Here are 7 possible usage examples for WP_User_Query
Example 1 – Searching only for administrators
Example 2 – Searching only for administrators and excluding users with ID 1, 2, 3
Example 3 – Searching for a user with the email email@email.com
Example 4 – Searching for a user with the login as email@email.com
Example 5 – Searching for the last 5 users who registered to our site
Example 6 – Searching for all the users with a meta key
Example 7 – Searching for all the users with a multiple meta keys
Good luck!
$user_query = new WP_User_Query(array( 'role' => 'Administrator' ));
Example 2 – Searching only for administrators and excluding users with ID 1, 2, 3
$user_query = new WP_User_Query(array( 'role' => 'Administrator', 'exclude' => array(1, 2, 3)));
Example 3 – Searching for a user with the email email@email.com
$user_query = new WP_User_Query(array( 'search' => 'email@email.com'));
Example 4 – Searching for a user with the login as email@email.com
$user_query = new WP_User_Query(array( 'search' => 'email@email.com', 'search_columns' => array('user_login')));
Example 5 – Searching for the last 5 users who registered to our site
$user_query = new WP_User_Query(array('number' => 5, 'orderby' => 'registered', 'order' => 'DESC'));
Example 6 – Searching for all the users with a meta key
$user_query = new WP_User_Query(array('meta_key' => 'something', 'meta_value' => 'good', 'meta_compare' => '='));
Example 7 – Searching for all the users with a multiple meta keys
$user_query = new WP_User_Query(array('meta_query' => array('relation' => 'OR', array('key' => 'country', 'value' => 'USA', 'compare' => '='), array('key' => 'country', 'value' => 'Canada', 'compare' => '='))));
Good luck!
0 comments:
Post a Comment
Thanks for commenting. I will Reply you soon