get_users()
云策文档标注
概述
get_users() 函数用于根据指定条件检索用户列表,基于 WP_User_Query 类实现。返回结果类型取决于 'fields' 参数设置,可以是 WP_User 对象、stdClass 对象或 ID 数组。
关键要点
- 函数接受一个可选的 $args 数组参数,用于定义查询条件,参数详情参考 WP_User_Query::prepare_query()。
- 返回值类型由 'fields' 参数控制:默认 'all' 或 'all_with_meta' 返回 WP_User 对象数组;设置为 wp_users 表字段数组时返回 stdClass 对象数组;设置为单个字段时返回 ID 数组。
- 函数内部使用 WP_User_Query 执行查询,并返回结果数组。
- 注意:当使用 meta_value 查询且值为空字符串时,查询可能降级为仅按 meta_key 搜索,需谨慎处理以避免安全漏洞。
- 在大型站点上,默认参数可能导致性能问题,建议合理设置查询限制。
代码示例
// 示例1:按角色查询用户
$blogusers = get_users( array( 'role__in' => array( 'author', 'subscriber' ) ) );
foreach ( $blogusers as $user ) {
echo '' . esc_html( $user->display_name ) . '';
}
// 示例2:使用搜索字段
$blogusers = get_users( array( 'search' => 'john' ) );
foreach ( $blogusers as $user ) {
echo '' . esc_html( $user->user_email ) . '';
}
// 示例3:查询特定字段
$blogusers = get_users( array( 'fields' => array( 'display_name' ) ) );
foreach ( $blogusers as $user ) {
echo '' . esc_html( $user->display_name ) . '';
}注意事项
- 确保对用户输入进行适当清理和验证,特别是在使用 meta_value 查询时,避免空字符串导致的查询降级问题。
- 在大型站点中,避免使用默认参数(如 number 为 -1 表示所有用户),以优化性能。
- WordPress 6.0 起,WP_User_Query 支持更多 fields 选项,更新时注意兼容性。
原文内容
Retrieves list of users matching criteria.
Description
See also
Parameters
$argsarrayoptional-
Arguments to retrieve users. See WP_User_Query::prepare_query() for more information on accepted arguments.
Default:
array()
Source
function get_users( $args = array() ) {
$args = wp_parse_args( $args );
$args['count_total'] = false;
$user_search = new WP_User_Query( $args );
return (array) $user_search->get_results();
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Skip to note 8 content
growthwp
Please note that if you search by `meta_value` and it ends up being `”` (an empty string), the query, which is really a wrap over the `WP_User_Query` class and hence this applies to other functions as well, ends up forfeiting the check for the `meta_value` and simply downgrades to searching by `meta_key` only.
Please be very careful when you have `meta_values` that are dynamic or that you can’t/don’t check for this exact case, if the list that you retrieve using this query is used for something important, you might end up with security holes.
“User input should be parsed”. Yes, but, user input should not be immediately `esc_html`’d or the like, escape at output, sanitize before queries and now that we know this, check for validity — but here lies the problem, we, as well as some people who’ve been with WP for 10+ years didn’t know about this behavior. A `preg_match` fixes it all, yes, but only if your assumptions are updated with this knowledge.
Additionally, this is not a case of “you just forgot to parse”, we parse everything that comes inside and had security audits on our core codebase pieces but just simply weren’t aware of this behavior and assumed we didn’t even need to parse.
I’ve opened a ticket about it if you’re interested in a PoC and how it affected us specifically: https://core.trac.wordpress.org/ticket/49641
Skip to note 9 content
crmunro
An example of fetching users that match any one of an array of roles using
role__in.array( 'author', 'subscriber' ) ) ); // Array of WP_User objects. foreach ( $blogusers as $user ) { echo '<span>' . esc_html( $user->display_name ) . '</span>'; }Skip to note 10 content
Codex
An example using the ‘search’ field.
'john' ) ); // Array of WP_User objects. foreach ( $blogusers as $user ) { echo '<span>' . esc_html( $user->user_email ) . '</span>'; }This example will find and display all users that have a user name, ID, email of “john”. You can also do wild card search by adding an * before or after your search query. For example, to search for all users that start with “jo”, you would pass something like “jo*”.
The results will be all users whose user names, IDs, or emails that start with “jo”. The * can be placed before or after your search query. When placed before, the results will be all users that end in your query.
Skip to note 11 content
Pepro Dev. Group
WP_User_Query now accepts fields options in WordPress 6.0
https://make.wordpress.org/core/2022/04/29/wp_user_query-now-accepts-fields-options-in-wordpress-6-0/
Skip to note 12 content
Codex
A basic example to display all subscribers in an unordered list.
' . esc_html( $user->user_email ) . '</span>'; }Skip to note 13 content
Codex
An example of querying by a specific field.
array( 'display_name' ) ) ); // Array of stdClass objects. foreach ( $blogusers as $user ) { echo '<span>' . esc_html( $user->display_name ) . '</span>'; }Skip to note 14 content
Kowsar Hossain
The default value of the
numberparameter is -1, which means it lists all the users. It can cause performance issues on larger sites, so it should be used with caution.