返回介绍

get_users()

发布于 2017-09-11 00:43:06 字数 4376 浏览 1016 评论 0 收藏 0

get_users( array $args = array() )

Retrieve list of users matching criteria.


description


参数

$args

(array) (Optional) Arguments to retrieve users. See WP_User_Query::prepare_query(). for more information on accepted arguments.

Default value: array()


返回值

(array) List of users.


源代码

File: wp-includes/user.php

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();
}

更新日志

Versiondescription
3.1.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/class-wp-user-query.php: WP_User_Query::__construct()

Used By

  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::column_users()
  • wp-admin/includes/ms.php: wpmu_delete_blog()
  • wp-admin/includes/schema.php: populate_network()
  • wp-admin/includes/ajax-actions.php: wp_ajax_autocomplete_user()
  • wp-admin/includes/ms.php: confirm_delete_users()
  • wp-includes/user.php: wp_dropdown_users()
  • wp-includes/author-template.php: wp_list_authors()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getAuthors()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getUsers()
  • Show 4 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    An example using the ‘search’ field.

    
    <?php
    $blogusers = get_users( array( 'search' => '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.

  2. An example of querying by a specific field.

    
    <?php
    $blogusers = get_users( array( 'fields' => array( 'display_name' ) ) );
    // Array of stdClass objects.
    foreach ( $blogusers as $user ) {
    	echo '<span>' . esc_html( $user->display_name ) . '</span>';
    }
    
    

    A basic example to display all subscribers in an unordered list.

    
    <?php
    $blogusers = get_users( 'blog_id=1&orderby=nicename&role=subscriber' );
    // Array of WP_User objects.
    foreach ( $blogusers as $user ) {
    	echo '<span>' . esc_html( $user->user_email ) . '</span>';
    }
    
    

    An example of fetching users that match any one of an array of roles using role__in.

    
    <?php
    $blogusers = get_users( [ 'role__in' => [ 'author', 'subscriber' ] ] );
    // Array of WP_User objects.
    foreach ( $blogusers as $user ) {
        echo '<span>' . esc_html( $user->display_name ) . '</span>';
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文