我正在创建一个用于在Wordpress中搜索用户的自动提示 问题是名字和姓氏都存储为meta_key。
因此无法进行“正常”搜索。
我可能要做两个查询。是否可以进行JOIN
?
这是我第一次尝试失败:
SELECT user_id, first_name, last_name
FROM wp_usermeta
WHERE (meta_key='first_name' OR meta_key='last_name' OR meta_key = 'user_email')
AND LOWER(meta_value) LIKE '%jon%'
答案 0 :(得分:3)
我找到了答案。我不确定这是否是最好的,但它确实有效:
SELECT a.id, b1.meta_value AS first_name, b2.meta_value as last_name
FROM wp_users a
INNER JOIN wp_usermeta b1 ON b1.user_id = a.ID AND b1.meta_key = 'first_name'
INNER JOIN wp_usermeta b2 ON b2.user_id = a.ID AND b2.meta_key = 'last_name'
WHERE (b1.meta_value LIKE '%st%' OR b2.meta_value LIKE '%st%' OR a.user_email LIKE '%st%')
答案 1 :(得分:0)
试试这个
SELECT um.user_id, um.meta_key, um.meta_value, u.user_email
FROM wp_usermeta AS um
INNER JOIN wp_users AS u ON (u.ID = um.user_id)
WHERE meta_key IN ('first_name', 'last_name')
AND (meta_value LIKE '%jon%' OR u.user_email LIKE '%jon%')
答案 2 :(得分:0)
你可以使用get_users()函数
// code to use
$args = array(
'meta_key' => 'first_name',
'meta_value' => 'SomeName',
'meta_key' => 'last_name',
'meta_value' => 'SomeName2',
);
$users = get_users( $args );
然后
foreach ($users as $user) {
echo '<li>' . $user->user_email . '</li><br>';
echo '<li>' . $user->display_name. '</li>';
//data you want to dispaly
/*
[ID] => 1
[user_login] => admin
[user_pass] => $P$Bxudi6gJMk2GRt2ed3xvZ06c1BPZXi/
[user_nicename] => admin
[user_email] => admin@host.com
[user_url] => http://localhost/
[user_registered] => 2010-06-29 07:08:55
[user_activation_key] =>
[user_status] => 0
[display_name] => Richard Branson
*/
}
更多信息:http://codex.wordpress.org/Function_Reference/get_users
祝你好运。