WP_User_Query example


        $prepared_args = apply_filters( 'rest_user_query', $prepared_args$request );

        $query = new WP_User_Query( $prepared_args );

        $users = array();

        foreach ( $query->results as $user ) {
            $data    = $this->prepare_item_for_response( $user$request );
            $users[] = $this->prepare_response_for_collection( $data );
        }

        $response = rest_ensure_response( $users );

        // Store pagination values for headers then unset for count query.
/** * Handles the users column output. * * @since 4.3.0 * * @param array $blog Current site. */
    public function column_users( $blog ) {
        $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
        if ( ! $user_count ) {
            $blog_users = new WP_User_Query(
                array(
                    'blog_id'     => $blog['blog_id'],
                    'fields'      => 'ID',
                    'number'      => 1,
                    'count_total' => true,
                )
            );
            $user_count = $blog_users->get_total();
            wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
        }

        
/** * Filters the query arguments used to retrieve users for the current users list table. * * @since 4.4.0 * * @param array $args Arguments passed to WP_User_Query to retrieve items for the current * users list table. */
        $args = apply_filters( 'users_list_table_query_args', $args );

        // Query the user IDs for this page.         $wp_user_search = new WP_User_Query( $args );

        $this->items = $wp_user_search->get_results();

        $this->set_pagination_args(
            array(
                'total_items' => $wp_user_search->get_total(),
                'per_page'    => $users_per_page,
            )
        );
    }

    
$args['orderby'] = $_REQUEST['orderby'];
        }

        if ( isset( $_REQUEST['order'] ) ) {
            $args['order'] = $_REQUEST['order'];
        }

        /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
        $args = apply_filters( 'users_list_table_query_args', $args );

        // Query the user IDs for this page.         $wp_user_search = new WP_User_Query( $args );

        $this->items = $wp_user_search->get_results();

        $this->set_pagination_args(
            array(
                'total_items' => $wp_user_search->get_total(),
                'per_page'    => $users_per_page,
            )
        );
    }

    

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

/** * Lists all the users of the site, with several options available. * * @since 5.9.0 * * @param string|array $args { * Optional. Array or string of default arguments. * * @type string $orderby How to sort the users. Accepts 'nicename', 'email', 'url', 'registered', * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'. * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'. * @type int $number Maximum users to return or display. Default empty (all users). * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false. * @type bool $show_fullname Whether to show the user's full name. Default false. * @type string $feed If not empty, show a link to the user's feed and use this text as the alt * parameter of the link. Default empty. * @type string $feed_image If not empty, show a link to the user's feed and use this image URL as * clickable anchor. Default empty. * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type. * @type bool $echo Whether to output the result or instead return it. Default true. * @type string $style If 'list', each user is wrapped in an `<li>` element, otherwise the users * will be separated by commas. * @type bool $html Whether to list the items in HTML form or plaintext. Default true. * @type string $exclude An array, comma-, or space-separated list of user IDs to exclude. Default empty. * @type string $include An array, comma-, or space-separated list of user IDs to include. Default empty. * } * @return string|null The output if echo is false. Otherwise null. */
null,
            $page_num
        );

        if ( null !== $url_list ) {
            return $url_list;
        }

        $args          = $this->get_users_query_args();
        $args['paged'] = $page_num;

        $query    = new WP_User_Query( $args );
        $users    = $query->get_results();
        $url_list = array();

        foreach ( $users as $user ) {
            $sitemap_entry = array(
                'loc' => get_author_posts_url( $user->ID ),
            );

            /** * Filters the sitemap entry for an individual user. * * @since 5.5.0 * * @param array $sitemap_entry Sitemap entry for the user. * @param WP_User $user User object. */
Home | Imprint | This part of the site doesn't use cookies.