Question

How do I get a list of all users with role = 'Customers' including all the metadata per user, means wp_users + wp_usermeta.

Query below does not generate the wanted results.

    $query = "SELECT * FROM wp_users INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id ORDER BY ID DESC');  ";

    $data = $wpdb->get_results($query,ARRAY_A);

But that list is not correct. It shows each user x times depend of the amount of rows in wp_usermeta.

How can i filter it? That each user shows per record all his user + metadata?

Still searching for 6 hours.

Was it helpful?

Solution

I think you should use wp-api functions which will do everything for you.

  • get_users() function will get all users data just define fields which u require.
  • get_user_meta() function will get usermeta data.
 
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach($users as $user){
        print_r(get_user_meta ( $user->ID));
    }

OTHER TIPS

At bit more formal way to achieve the same results are as follows Substitute any role as necessary and this code assumes you are using woocommerce for more detailed information.

function GetSubscriberUserData()
{
$DBRecord = array();
$args = array(
    'role'    => 'Subscriber',
    'orderby' => 'last_name',
    'order'   => 'ASC'
);
$users = get_users( $args );
$i=0;
foreach ( $users as $user )
  {
  $DBRecord[$i]['role']           = "Subscriber";   
  $DBRecord[$i]['WPId']           = $user->ID;  
  $DBRecord[$i]['FirstName']      = $user->first_name;
  $DBRecord[$i]['LastName']       = $user->last_name;
  $DBRecord[$i]['RegisteredDate'] = $user->user_registered;
  $DBRecord[$i]['Email']          = $user->user_email;

  $UserData                      = get_user_meta( $user->ID );  
  $DBRecord[$i]['Company']        = $UserData['billing_company'][0];    
  $DBRecord[$i]['Address']        = $UserData['billing_address_1'][0];
  $DBRecord[$i]['City']           = $UserData['billing_city'][0];
  $DBRecord[$i]['State']          = $UserData['billing_state'][0];
  $DBRecord[$i]['PostCode']       = $UserData['billing_postcode'][0];
  $DBRecord[$i]['Country']        = $UserData['billing_country'][0];
  $DBRecord[$i]['Phone']          = $UserData['billing_phone'][0];
  i++;
  }
return DBRecord;
}

I created a simple, free plugin, called Flat Meta Data, that flattens post and user meta data into a CSV that can more easily be used for reporting and analysis. In the process it creates tables in your database ([:prefix]postmeta_flat and [:prefix]usermeta_flat) that contain the same information and can be used in more complex queries. These tables have one row per post or user and one column per data point (meta_key) containing the meta values (meta_value).

After installing and activating the plugin (in wp-content/plugins/flat-table), it is accessible through a link in the main menu of the admin panel, called Flat Meta Data. You can then click on either the "Download post meta data" or "Download user meta data" button to generate the table and download the CSV containing all of the data in spreadsheet form.

A few notes:

  1. This plugin is provided free of charge. Use at your own risk.
  2. CREATE TABLE privileges are required. This shouldn't be an issue, but could be if you've restricted access to the WordPress database user.
  3. Generating and downloading a complete set of meta data can negatively impact system performance. Please do so only during off-hours or, better yet, on a sandbox populated with your live data.

Please let me know if you have any questions. Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top