Monday, March 17, 2014

7 Search Patterns Using WP_User_Query

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
 
$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!

A blogger

I am passionate blogger cum B.Tech. Computer engineering graduate. I love writing blog post. I spend my free time in writing blog post that will useful to everyone (including me). I have had some success making money blogging and want to help others do the same. I just figured that by creating a great and free resource a lot of links would follow – and they have. Some people ask me how they can repay me – which is not necessary - but for those wanting to show their appreciation, I just say linking to the article from their blog is the best compensation I could receive. Thanks for reading!

0 comments:

Post a Comment

Thanks for commenting. I will Reply you soon

 

Copyright @ 2013 PHP WORDPRESS MAGENTO .

Designed by Kcon Technosoft