🎬 Introduction
If you’ve read the previous few issues of The WP Minute, then you know what we’re doing. And if you haven’t, I’ll make it simple: We’re seeing how to customize the All Users page by listing users custom metadata and built-in features of WordPress.
Ideally, this can or should be used whenever you have your own metadata with which you want to work; however, since it’s hard to get everyone on the same page as to do that, we’ll look at how we can achieve the same results using metadata WordPress already makes available.
2️⃣ Using Two Pieces of Metadata
Let’s take our example a step further and say we want to query off of two pieces of user metadata and order by one of them.
Following the format we used above, let’s say in addition to having a user_status
we also have a acme_user_deleted
meta key that is used to indicate the user has been “soft deleted.” That is, we can use this flag to determine if the user remains in the system but not as an active user. This is helpful for whenever we may need to restore a user or continue to retrieve data after they’ve been removed.
Anyway, the meta key for this will be acme_user_deleted
and the value will be 1
if they have been soft deleted; otherwise the value will not exist. So to implement a query for something like this, we’ll add the following to our meta query:
'user_deleted' => [
'key' => 'acme_user_deleted',
'compare' => 'NOT EXISTS'
]
Code language: PHP (php)
Notice here that I’m using the compare
array key with the value of NOT EXISTS
. This tells WordPress “only include users where the meta value does not exist for the specified meta key.
An implementation of this code along with another piece of metadata from earlier will look like this:
add_action(
'pre_get_users',
function ($query) {
if (!isOnUsersPage()) {
return $query;
}
$metaQuery = [
'user_deleted' => [
'key' => 'acme_user_deleted',
'compare' => 'NOT EXISTS'
],
'user_status' => [
'key' => 'status'
]
];
$query->set('meta_query', $metaQuery);
$query->set('orderby', 'user_status');
$query->set('order', 'desc');
return $query;
}
);
Code language: PHP (php)
Now when you run this query, you’ll get back all of the users that do not have the acme_user_deleted
meta key as well as all of the users with the status
key returned in descending order.
💡 But Wait!
Just as with the first example, it’s hard to test this code without using two pieces of pre-existing metadata. So let’s continue to use the user_nickname
and then also use their lastname
and we’ll mix it up a little bit so that it sorts the user by their last name in ascending order.
To do this, use the following code:
add_action(
'pre_get_users',
function ($query) {
if (!isOnUsersPage()) {
return $query;
}
$metaQuery = [
'user_nickname' => [
'key' => 'nickname'
],
'user_lastname' => [
'key' => 'last_name'
]
];
$query->set('meta_query', $metaQuery);
$query->set('orderby', 'user_lastname');
$query->set('order', 'asc');
return $query;
}
);
Code language: PHP (php)
If you run this code within your plugin, your All Users page should, by default, list all users in the database in alphabetical order based on their last name and it will include the nickname as part of the query, too.
🏁 Conclusion
The final code should look something like this:
add_action(
'pre_get_users',
function ($query) {
if (!isOnUsersPage()) {
return $query;
}
$metaQuery = [
'user_deleted' => [
'key' => 'acme_user_deleted',
'compare' => 'NOT EXISTS'
],
'user_status' => [
'key' => 'status'
]
];
$query->set('meta_query', $metaQuery);
$query->set('orderby', 'user_status');
$query->set('order', 'desc');
return $query;
}
);
Code language: PHP (php)
Working with custom queries on the public facing area of a WordPress post, page, or custom post type is easy to do once you understand how to use WP_Query
. That’s a post in and of itself and there are already a number of articles I’ve written, that are available across the web, and that are available throughout the WordPress Development resources.
Part of learning how to work with WP_Query
is also learning how to work with a subset of its features such as WP_Meta_Query
as it allows us to easily work with data with tables that are, say, adjacent or related to the data with which we may be working.
Further, it allows us to customize how some of the standard WordPress views may work within the public aspect of the site, the administration area of the site, and even data retrieved via the REST API.
And that’s what the purpose of this article is meant to show: How to retrieve records in a customized way by using built-in WordPress API functions that don’t require writing custom SQL.
Join The Newsletter
Get your favorite 5 minutes of WordPress news for busy professionals every week — 100% Free! Join the WP Minute Newsletter below 👇