I have a site with many users and many of them have the same last name. I want to get the emails of all the users with the same last name (IE: Smith) that has a post related to a particular taxonomy term (IE: Baseball).
So far I have this code that works great in getting all the users with the same last name ( thanks to Mike Schinkel ). I suck at using the JOIN function but I am learning and I really need this sooner than later, so I need help.
$sql =<<<SQL
SELECT
{$wpdb->users}.user_email,
{$wpdb->usermeta}.meta_value
FROM
{$wpdb->users}
LEFT JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id
WHERE 1=1
AND {$wpdb->users}.user_status = '0'
AND {$wpdb->usermeta}.meta_key = 'last_name'
AND {$wpdb->usermeta}.meta_value = 'Smith'
SQL;
$usersemails = $wpdb->get_results($sql);
header('Content-type:text/plain');
print_r($usersemails);
Your time is greatly appreciated and I will pay it forward. Thanks.
Hi @Holidaymaine:
Here’s the query you are looking for: