WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can i use multiple meta_key and meta_value to search users?

For example, i want to search & find user name is "David" & location in "London".

I'm using this query, But nothings in result!

$query = "SELECT user_id FROM $wpdb->usermeta WHERE (meta_value LIKE '%%david%%') AND (meta_key = 'first_name') AND (meta_value LIKE '%%london%%') AND (meta_key = 'location')";

$rows = $wpdb->get_results($query);


share|improve this question
up vote 1 down vote accepted

Your query is wrong that's way noting is returned because there is no row in the database that holds more then one meta_key. What you can do instead is create a Sub Query to get all user id's of users who live in London as a Sub Query and the use that to filter users id's of users who are named David, something like this:

$query = "SELECT user_id FROM $wpdb->usermeta
    WHERE (meta_value LIKE '%%david%%') 
    AND (meta_key = 'first_name') 
    AND user_id IN (SELECT user_id FROM $wpdb->usermeta WHERE (meta_value LIKE '%%london%%') AND (meta_key = 'location'))";

$rows = $wpdb->get_results($query)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.