[wp-hackers] Select from meta with a specific value or no meta

Lox lox.dev at knc.nc
Tue May 24 07:36:30 UTC 2011


Hello,

I need to make a query that selects users having a meta with a specific
value OR not having that meta. I don't see how to achieve it...

This will return me the users with the meta 'newsletter_optin' set to '1',
But I also need the users who do have not that meta set.

SELECT ID FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON
({$wpdb->users}.ID = {$wpdb->usermeta}.user_id)
INNER JOIN {$wpdb->usermeta} AS mt1 ON ({$wpdb->users}.ID = mt1.user_id)
WHERE 1=1
AND {$wpdb->usermeta}.meta_key = 'newsletter_subscribe'
AND CAST({$wpdb->usermeta}.meta_value AS CHAR) = '1'
AND mt1.meta_key = 'newsletter_optin'
AND CAST(mt1.meta_value AS CHAR) = '1'

Regards.

-- 
Lox
lox.dev at knc.nc


More information about the wp-hackers mailing list