Search Users base on meta_value & meta_key

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”.

Read More

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

Related posts

Leave a Reply

1 comment

  1. 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)