[英]WordPress: SELECT wp_users and all associated meta data from wp_metadata into one row per user record
This is how I've been able to extract all of the relavant data into a single row for each user
record: 这就是我能够将每个
user
记录的所有相关数据提取到单个行中的方式:
SELECT user_login, user_pass, user_email, user_registered,
meta1.meta_value AS billing_address_1,
meta2.meta_value as billing_address_2,
meta3.meta_value as billing_city,
meta4.meta_value as billing_first_name,
meta5.meta_value as billing_last_name,
meta5.meta_value as billing_postcode,
meta5.meta_value as billing_country,
meta5.meta_value as billing_state
FROM wp_users users,
wp_usermeta meta1,
wp_usermeta meta2,
wp_usermeta meta3,
wp_usermeta meta4,
wp_usermeta meta5,
wp_usermeta meta6,
wp_usermeta meta7,
wp_usermeta meta8
WHERE users.id = meta1.user_id
AND meta1.meta_key = 'billing_address_1'
AND users.id = meta2.user_id
AND meta2.meta_key = 'billing_address_2'
AND users.id = meta3.user_id
AND meta3.meta_key = 'billing_city'
AND users.id = meta4.user_id
AND meta4.meta_key = 'billing_first_name'
AND users.id = meta5.user_id
AND meta5.meta_key = 'billing_last_name'
AND users.id = meta6.user_id
AND meta6.meta_key = 'billing_postcode'
AND users.id = meta7.user_id
AND meta7.meta_key = 'billing_country'
AND users.id = meta8.user_id
AND meta8.meta_key = 'billing_state'
However, at this point (and I haven't even listed half of the meta data that needs to be extracted), phpMyAdmin is telling me that my JOIN
is too big ... 但是,在这一点上(并且我什至没有列出需要提取的元数据的一半),phpMyAdmin告诉我我的
JOIN
太大...
Is there a more efficient way of doing this? 有更有效的方法吗?
If you need a user with all details then you can simply use get_userdata function as given below 如果您需要一个具有所有详细信息的用户,则可以简单地使用get_userdata函数,如下所示
$user_info = get_userdata(1); // 1 is user ID here (required)
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User level: ' . $user_info->user_level . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
// more...
If you want to get the details of currently logged in user then you can use get_currentuserinfo function 如果要获取当前登录用户的详细信息,则可以使用get_currentuserinfo函数
global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
You can use the get_user_meta()
function. 您可以使用
get_user_meta()
函数。 It will return all of the user meta information for a specific user. 它将返回特定用户的所有用户元信息。 Call the function for each user, in a loop.
循环调用每个用户的函数。
http://codex.wordpress.org/Function_Reference/get_user_meta http://codex.wordpress.org/Function_Reference/get_user_meta
如何创建视图并在其上运行选择呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.