How to display, search and filter WordPress user meta data
In this tutorial we will show you how to add, edit and retrieve WordPress user meta data associated with a particular user in to your WordPress admin area of your site using custom fields.
The WordPress user meta API allows you to store any data you might wish to associate with a particular WordPress user. Many plugins use the WordPress user meta to store additional information for the users that WordPress doesn’t store by default. Sometimes you might also need to store custom user data, such as registration forms data and the WordPress user meta is perfect for this job.
However WordPress doesn’t provide an option to display the user meta data, which makes it difficult to retrieve the information that has been stored. This is where the Users Insights plugin can come in very useful.
For this purpose you can use the Custom User Fields feature of Users Insights. With this feature you can easily create custom user fields that you can update from the user profile section, as well as display and filter in the WordPress user table.
The Custom User Fields feature works with the WordPress User Meta API, which means that it stores the data as user meta and it can also retrieve any existing user meta.
In order to load your custom user meta data on the user table, just register a custom user field with the same meta key as your user meta field.
What is metadata?
Before diving into our use case, it’s important to understand what metadata is. Usually, metadata refers to data about other pieces of data. They play an important role in improving organization, clarity and performance, not just in the software industry.
For example, if you ever went to a library you probably have seen traditional card catalogues. They are used to hold information about books, such as author, title, location, publisher, category.
But at this point things may not be that clear for you. What is metadata in my WordPress site? How do I know I have it? How can I use it?
Well, just like our old card catalogues WordPress has its own way of storing metadata. WordPress user meta is data that is attached to users in the database. That is done using custom fields and post meta. And in our case in particular, the useful metadata we have is stored as custom user fields.
These are data points about your data (users) that can help you better understand them. Crucial information such as your customer location and their lifetime value can be fetched from other WordPress plugins and stored automatically.
Now let’s see a few metadata examples and how to use it.
Metadata examples and common usage
Let’s make our concept of what metadata is even clearer with a few examples. In our WordPress concept metadata can be requested, collected from other plugins, or gathered automatically.
It’s possible to request user information, such as their Job Title, company size, or even alternate email address. These metadata examples can be used for marketing campaigns, different access levels, account recovery and more.
In case you are using other plugins, it’s very likely that they collect a lot of metadata already. These are often custom user fields for purchase information, courses, activity. The amount of metadata available depends on the plugins you are using, but often plugins rely heavily on them.
How to add custom user meta
User meta is specified as a key-value pair (meta key). Currently, you have two ways to add this information: 1) Add custom user meta field manually from Users Insights or 2) Automatically load existing user meta from a WordPress plugin or a theme.
With Users Insights adding custom user meta to your WordPress users it’s actually really easy to do. Let’s say, for example, that you want to store the “Job Title” for each WordPress user. You might be using a plugin to store the user meta or just store it by yourself (with code)by using the add_user_meta() and/or the update_user_meta() WordPress functions:
<?php add_user_meta( $user_id, 'job_title', $job_title, true ); ?>
In order to retrieve the data saved as “job_title” user meta, all you need to do is register a custom user field with the “job_title” key. You can do this from the Users Insights -> Custom Fields section:
In this example we set the field type as “Text”, as the Job Title is a text field type. This means that Users Insights will provide text-based filters in the WordPress user table, such as “contains” or “starts with”. You can use this data later to filter users or display additional user related information in the WordPress dashboard.
How to display WordPress user meta in the admin
Once the field is created, Users Insights will get the user metadata from the WordPress database and it will be available in the fields section of the Users Insights page. You can show the new user meta data by making the “Job Title” field visible on the table by clicking on the “Eye icon” and checking the “Job Title” option.
Once visible, you can display all the stored user meta data for this field on the table:
You can also sort your table based on this field data (alphabetically):
Besides the user table, Users Insights also displays the custom user meta fields in the user profile section. In this section you can view all the existing user meta, as well as edit the custom meta fields data.
How to change wordpress user meta
If you want to change the user meta data in the newly created custom field, you can do it via the Users Insights profile page. This is where all the custom fields are loaded. If the custom field in question can be edited, it will have edit button on the right side of the field. If you click the edit button you will be able to change the user meta data of the specific field and save the new value:
If you don’t know the meta keys used
If you are not sure about what meta keys are used to store your custom user meta, you can use the Extended CRM For Users Insights plugin. This is a free add-on that provides some extended features for Users Insights.
One of the features that it provides is listing all the existing user meta keys in the Custom Fields section. With this option you can find the meta keys that your scripts/plugins use to store the data and select them from the list when creating a custom field.
Available data types in the user table and filters
The most common data type that is stored in the user meta is the standard text data type. This can be any kind of information that is stored in a text format. An example would be a person’s job title that is stored as a simple text. Here is an example where we search for all users, whose job title is any kind of designer:
If you want to learn more about the Users Insights Filters, you can visit the Smart Filters page that describes this feature in detail.
In addition to the standard text field types, Users Insights also provides a number field type, that can be used with any user meta that stores numeric data.
If, for example, you store your users age as user meta with a meta key “age”, you can just create a custom user field with the “age” key and select the “Number” field type.
Once created, just like the example above, you can display the data stored for this field, as well as sort and filter the user table by this field’s data. For the numeric field types you can use filters like “is bigger” or “is smaller” to find the users that fulfil a certain criteria.
Another field type that Users Insights supports is a date field type. This field type is read-only, which means that it can be primarily used to retrieve existing user meta data, stored in a date format. When a custom user meta field is registered as a date field, you will be able to sort the user table by this field in a chronological order.
Here is an example where we have applied a filter to our custom date field to find all the users that were born after a selected date, with the user table sorted by this field in an ascending order.
The dropdown field type allows for a selection of values in the user profile edit screen. This is a great option to save custom user fields with preset values. Then, instead of a simple text field, you can restrict the possible choices. This makes your user update process much easier and makes the user filtering more precise.
When you have a dropdown, you avoid typos and makes sure similar words are grouped inside a single option. Thus, instead of having different options for “web design”, “webdesign” and “web designer”, you have just one. Then when you filter your users, you know exactly which wording was picked.
This custom field has a special setup. Under Users Insights > Custom Fields, you select not only the field name and key but the options as well.
Once this field is saved, your user profiles will have it. Then you can select which option applies to which user:
As expected, the filters for this kind of field come auto-populated. This allows for searches including is/is not or is set/ isn’t set. This is how a usual user search looks like:
WordPress user meta management
After your custom fields are defined, mapped and populated, we can use them. Therefore, now starts the WordPress user meta management work.
This process starts by thinking about how we get user meta in the first place, and how you want to use it. It may be useful to have a WordPress meta description document, for example, thus you know its scope. It can be used as a guide to show your team what to do with your fields. For example, a “freelance availability” field can be used when you have new projects to pass on. On the other hand, fields like location, country, and city have broader use. They can be used as modifiers for your data, such as defining available developers just in your city.
Another important aspect of your WordPress meta management is about having your database updated. For example, let’s say you have a gravity form to collect your user availability. This data is in your user profile, but you can’t edit it. That’s because fields loaded by your Users insights modules are protected.
To allow edit operations of this custom user field, you need to map it under Users Insights > Custom fields. But then you have two instances of that data. Now what you can do is to simply use the “profile settings” option in the top right corner to hide your duplicates. Just click the eye icon and they are gone.
In that same section, you can reorder sections and add new ones. This allows you to get much better use of your WordPress user meta.
How to search custom user meta fields data
One of the most useful features is that you can filter your WordPress user list by the custom user meta, so you can easily search for your users that have a specific value selected for this field.
Let’s look into more advanced ways to apply this tool, using Users Insights and WordPress to search custom fields beyond the simple parameters. There are many different filtering methods you can use to search user meta fields. For text-type fields you can use the following operators:
- Contains / Does not contain – searches for the string anywhere in your field
- Is / Is not – exact match
- Starts with / Ends with – checks only the beginning or the end of your field
- Is set / Is not set – checks only if that user has or hasn’t that field, no matter the contents
With these possibilities in mind you can freely search custom fields data. For example, you may want to search for users with a job title that is not designer:
It’s also possible to combine them to better search user metadata. If you want to search for users who have a “senior” job title, but also who work in a management job you can combine these for a specific query:
When it comes to searching WordPress user meta, the type of the data stored is very important. So, for number-based fields you have a different set of possibilities. Here is a list of operators you can use to search user meta if the data is numbers:
- Is – exact match
- Is smaller than
- Is bigger than
- Is set / is not set
The number fields are great to search custom fields and find data in ranges. So, for example, if you want to find all users with ages between 15 and 30 you can use this set of filters:
You may notice that in your results list, user meta fields don’t have the values of 15 and 30. That’s because this is a bigger than X, not bigger or equal than X. Therefore, if you want to include 15 (or any other number) you need to use a different value (such as 14).
The date ranges are another data type that can be stored in user meta. Like the numeric type, dates can be used to find all users within a certain range. In addition, it’s possible to search meta fields in comparison to the current date. Here are all the options for date fields:
- Is more / less than X days ago
- Is exactly X days ago
- Is after / before date X
- Is on date X
- Is set / Is not set
Here is an example of using two different date filters together:
Just remember that when you use date to search users by meta the “edges” of your search aren’t included. So, if we want to find all users who were born on or after Jan 1st 1980, we would need to use 31st December 1979 in the search criteria.
Exporting the WordPress user meta data
With the Users Insights Export feature you can also export your WordPress user meta data in a CSV format. The export feature allows you to export the data with the same settings applied to the user table, including all the filters applied, visible fields selected and sorting options. This allows you to create a fully customized user export.
With the example above, where we have applied a filter to find all the users that are older than 30, in order to export the data generated on the table, we just need to click on the Export button. This will generate a CSV file with the same exact data:
WordPress user meta is very useful for storing custom data about users. You can use this data to filter users or display additional user related information in the WordPress dashboard. In this article we showed how to add, edit and retrieve user meta values associated with a particular user in to your WordPress admin area of your site using custom fields and Users Insights.