[wp-hackers] WP User Query with two meta keys

Mike Little wordpress at zed1.com
Tue May 1 19:36:46 UTC 2012


On Tue, May 1, 2012 at 18:14, Otto <otto at ottodestruct.com> wrote:

> On Tue, May 1, 2012 at 11:27 AM, Robert Lusby <nanogwp at gmail.com> wrote:
> > Is it possible to select a user matching "two" meta keys and values, in
> WP
> > User Query?
>
> Not with WP_User_Query, no.
>
> > If not - how would you go about this?
>
> Direct SQL queries is the only way I know of. There isn't any support
> for it in the core objects at present.
>
>
You can cheat and use the WP_Tax_query class.
Create your $tax_query array of arrays like you would for a post query,
then instantiate a WP_Tax_Query.

$my_query = new WP_Tax_Query( $tax_query );
Then get the the SQL like this
$sql = $my_query->get_sql( $wpdb->users, 'ID' );
You then have the bitt to add to your query: use $sql['join'] and
$sql['where']



> You could extend the WP_User_Query class to add support for more fine
> grained meta queries, but again, you'd be writing new SQL in that
> class.
>
>
I'm working on a project now in BuddyPress which doesn't use the the
WP_User_Query class otherwise I might already have an example for you.


Mike
-- 
Mike Little
http://zed1.com/


More information about the wp-hackers mailing list