我有一个有很多用户的网站,其中很多人都有相同的姓氏。我想获得所有姓氏相同(即:Smith)的用户的电子邮件,这些用户的邮件中有一个与特定分类术语(即:棒球)相关的帖子。
到目前为止,我有这段代码,它可以很好地让所有用户使用相同的姓氏(感谢Mike Schinkel ). 我很难使用连接函数,但我正在学习,我真的很需要它,所以我需要帮助。
$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);
非常感谢您的时间,我将提前支付。谢谢
最合适的回答,由SO网友:MikeSchinkel 整理而成
你好@Holidaymaine:
以下是您要查找的查询:
<?php
include( \'../wp-load.php\' );
$sql =<<<SQL
SELECT DISTINCT
u.user_email AS user_email,
um.meta_value AS user_lastname
FROM
{$wpdb->users} AS u
LEFT JOIN {$wpdb->usermeta} AS um ON u.ID = um.user_id
LEFT JOIN {$wpdb->posts} AS p ON u.ID = p.post_author
LEFT JOIN {$wpdb->term_relationships} AS tr ON p.ID = tr.object_id
LEFT JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id
WHERE 1=1
AND u.user_status = \'0\'
AND um.meta_key = \'last_name\'
AND um.meta_value = \'%s\'
AND t.slug = \'%s\'
SQL;
$sql = $wpdb->prepare( $sql, \'Smith\', \'baseball\' );
$usersemails = $wpdb->get_results( $sql );
header( \'Content-type:text/plain\' );
print_r( $usersemails );