How to get a list of all WordPress Users and their UserData

How to get a list of all WordPress Users and their UserData

Due to the way WordPress likes to store User Data (including meta data) in its database there are a few quirks to how you need to get Users and their User Data. There are three native WordPress functions which you can use to do get a list of all the users in your site which are,

  • WP_USER_QUERY
  • get_users() and get_user_meta()

Table of Contents

How are the Users and their Data in WordPress stored

WordPress takes advantage of MYSQL’s core aspect of being a relational database and splits all User Data across two tables, those being the wp_users and wp_usermeta tables.

In addition, and importantly, each meta gets its own row and NOT column in the usermeta table. This has some annoying repercussions for extracting this data later (CON) but it does make it easy to add meta on the fly (PRO) because you don’t need to add a new column. Adding a new column would require you to add a column in the MYSQL table and, depending on how you do it, could cause you to have an error if you run the code without first adding the column. As most WordPress are not developers this makes it less usable to the user base.

Pros and Cons of WordPress’s default method of storing Users and User Meta Data

This is relevant to this articles’ focus on getting User Data because saving a meta data as a row instead of a column means that you can’t just do a simple join statement in MYSQL in order to get both User and Usermeta data.

If you tried this you would get something like this which has multiple rows for the same user which makes looping through the data difficult.

SELECT * FROM wp_users LEFT JOIN wp_usermeta ON wp_users.ID=wp_usermeta.user_id;

As such WordPress gives you some alternative but not very satisfying functions to get around this. We’ll go through these int he next section.

Various ways to get User and User Meta Information

Using two Native WordPress Functions

Given the structure of the meta data in the table WordPress gives you two functions which you could use to get both the data int he user table and the user meta table.

You could do something like the below for single or multiple users. Make sure to read the notes below on these functions.

<?php
    
    $allUserData = array();
    $users = array(12,13,14,15);
    $specificUsers = get_users( array( 'include' => $users ) );
    
    // $oneUser = get_user_by( 'ID', 123 ); // or you could get just one user like this
    foreach( $users as $user ){
        $eachUserMeta = get_user_meta($user->ID);
    }

    // $oneUserMeta = get_user_meta($user->ID); // or this if for oneUser

This works well enough until you hit a large amount of users that you are trying to access because you have to use a function call inside a loop before you even get all the data you need.

It wouldn’t be unheard of to have to loop through another array or set of objects to actually do something useful with the loop. Yet another barrier to page load speed before the HTML can render.

You can read more about the get_users function() here and the get_user_mate() here.

Using WP_USER_QUERY Class to get Users by particular meta data

The alternative is to get Users which have certain User Meta Data only. You can actually do this with the above get_users function as well as the specific WP_USER_QUERY class which WordPress specifically gives us to do complicated User Queries on the database.

So you could do something like this in order to get Users by a specific meta data and the User Id’s.

<?php
    $user_query = new WP_User_Query( 
        array( 
            'include' => array( 1, 2, 3 ),
            'meta_query' => array(
		'relation' => 'OR',
			array(
				'key'     => 'country',
				'value'   => 'New Zealand',
	 			'compare' => '='
			),
			array(
				'key'     => 'age',
				'value'   => array( 20, 30 ),
				'type'    => 'numeric',
				'compare' => 'BETWEEN'
			)
	    )
        )
    );

To explain what this is doing a little more,

  • We are getting Users 1,2 and 3 (3 users total)
  • AND that have ages (recorded as meta data) between 20 and 30
  • AND have country (recorded also as a meta data) of ‘New Zealand’.

NOTE: You could also remove the include part to get all users with these meta.

NOTE: This won’t give you all of the meta for these users though.

You can use WP_USER_QUERY in lots of different ways and can read more about it all here.

Learn WordPress customization throughly with an online course at Udemy

If you are having difficulty understanding this tutorial and then I suggest you start from scratch with WordPress in general. The best way to learn is with a better all around general understanding of WordPress. Check out some of the courses on Udemy related to WordPress. They cover developer level courses and more non-dev CMS editor type tutorials as well.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *