Rob
Rob

Reputation: 6380

Display a list of all the users using wp_query?

Is it possible to use wp_query to create a list of all the Wordpress users for a site and their data? If so, how?

I want to be able to list ALL the users (the site is open to new registrations) and all their details such as name, username etc as well as some custom meta's that I've added in the format:

function my_user_contactmethods($user_contactmethods) {

$user_contactmethods['Address'] = __('Address');
$user_contactmethods['Website'] = __('Website');
$user_contactmethods['Phone'] = __('Phone');
$user_contactmethods['Nationality'] = __('Nationality');
$user_contactmethods['Registering as'] = __('Registering as');
$user_contactmethods['Submit work'] = __('Submit work?');
$user_contactmethods['Attend'] = __('Attend?');
$user_contactmethods['Volunteer'] = __('Volunteer?');

// etc for each field you want to appear

return $user_contactmethods;

}

Or if there's another way to do it then I'm completely open.

Upvotes: 1

Views: 8698

Answers (2)

Jared Rice
Jared Rice

Reputation: 445

Looks like the correct way of querying users is with WP_User_Query. Seems very similar to how WP_Query works besides the foreach loop:

<?php
$args = array(
    .
    .
    .
);

// The Query
$user_query = new WP_User_Query( $args );

// User Loop
if ( ! empty( $user_query->get_results() ) ) {
    foreach ( $user_query->get_results() as $user ) {
        echo '<p>' . $user->display_name . '</p>';
    }
} else {
    echo 'No users found.';
}
?>

Here is the full documentation and list of properties available

Upvotes: 1

ksg91
ksg91

Reputation: 1299

If you are okay with not using wp_query , you can use get_users and get_user_meta to retrieve list of all users and their custom data.

For example,

<?php
    $blog_id = 1; //blog id
    $str = 'blog_id='.$blog_id;
    $users = get_users($str);
    $list = array();
    $i=0;
    foreach ($users as $user) {
       $list[$i]['id']=$user->ID;
       $list[$i]['email']=$user->email;
       $list[$i]['custom-1']= get_user_meta( $$user->ID, 'custom-1', true );
       $i++;
    }

?>

You can retrieve the fields that you want. Or you can just add all data and attach your custom fields with them.

Upvotes: 4

Related Questions