[wp-hackers] Extending the user search

John Blackbourn johnbillion+wp at gmail.com
Thu Mar 12 07:52:18 GMT 2009

I guess this is really an SQL question but I thought I'd ask here.

I'm writing a plugin which extends the user search/filter
functionality on the Users admin screen. It allows a user to filter
the results based on two custom user meta values. The [simplified] SQL
which retrieves the users is:

SELECT * FROM wp_users, wp_usermeta WHERE wp_usermeta.meta_key =
'key1' AND wp_usermeta.meta_value = 'value1'

That's all well and good for one meta value, but I am absolutely
stumped about how I can test for a second meta key/value pair which
must also be present. The problem is that the second meta_key will
have a different value to the first, and adding something like:

AND wp_usermeta.meta_key = 'key2' AND wp_usermeta.meta_value = 'value2'

will always return zero rows because you're trying to retrieve a row
with two different values for the same field (meta_key = 'key1' AND
meta_key = 'key2').

Any SQL experts out there that could give me a pointer?


More information about the wp-hackers mailing list