Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);

Thanks

share|improve this question

1 Answer 1

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

 
discard

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.