嗨,我正试图通过SQL查询从数据库获得特定的用户。也在尝试添加一些条件。User role name is: Student
但是我不能得到正确的结果与我的代码。
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','role') AND (meta_value NOT LIKE '%Student' AND u.user_email NOT LIKE '%@yourdomain.com')
您可以使用此函数按角色获取用户数组。
# This goes in functions.php
function get_users_by_role($role, $orderby, $order) {
$args = array(
'role' => $role,
'orderby' => $orderby,
'order' => $order
);
$users = get_users( $args );
return $users;
}
那么你可以这样使用:
$users = get_users_by_role('student', 'user_nicename', 'ASC');
或者像这样用get_users()
$students = get_users( array( 'role__in' => array( 'student' ) ) );
或
$students = get_users( 'role=student' );
或user_query way
$user_query = new WP_User_Query( array( 'role' => 'Student' ) );
如果你坚持使用SQL,那么你可以使用
SELECT wp_users.ID, wp_users.user_nicename
FROM wp_users INNER JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = 'wp_capabilities'
AND wp_usermeta.meta_value LIKE '%student%'
ORDER BY wp_users.user_nicename