在这种情况下,最好(IHMO)使用$wpdb class
例如:
<?php
global $wpdb;
//first get a list of all meta keys
$keys = $wpdb->get_col("SELECT distinct meta_key FROM $wpdb->usermeta");
//then prepare the meta keys query as fields which we\'ll join to the user table fields
$meta_columns = \'\';
foreach ($keys as $key) {
$meta_columns .= " MAX(CASE WHEN um1.meta_key = \'$key\' THEN um1.meta_value ELSE NULL END) AS ".str_replace(\'-\',\'_\',$key).", \\n";
}
//then write the main query with all of the regular fields and use a simple left join on user users.ID and usermeta.user_id
$query = "
SELECT
u.ID,
u.user_login,
u.user_pass,
u.user_nicename,
u.user_email,
u.user_url,
u.user_registered,
u.user_activation_key,
u.user_status,
u.display_name,
".rtrim($meta_columns,", \\n") ."
FROM
$wpdb->users u
LEFT JOIN
$wpdb->usermeta um1 ON (um1.user_id = u.ID)
GROUP BY
u.ID";
$users = $wpdb->get_results($query,ARRAY_A);
var_dump($users);
请记住,这是一个您应该构建的示例,例如过滤不需要的用户和元键。