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 into your WordPress admin area of your site using custom fields.
The WordPress user meta API allows you to store any data you 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 metadata, 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?
Metadata is additional information or data associated with various content types, such as posts, pages, users, attachments, and custom post types. Metadata is used to store and retrieve custom, structured information beyond the default content, allowing for more flexibility and customization in WordPress websites.
Metadata consists of key-value pairs, where each key is a unique identifier, and the corresponding value is the data associated with that key. This data is used to provide context, categorization, or additional details for content items.
For example, if you ever visited a library, you probably have seen traditional card catalogs. They hold book information, such as author, title, location, publisher, and category.
Like our old card catalogs, 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. In our case, in particular, the valuable metadata we have is stored as custom user fields.
Metadata in WordPress is typically managed and accessed through functions, custom fields, and APIs provided by WordPress or through the use of plugins and themes. It enables website administrators and developers to organize, categorize, and display content in a more structured and customized manner. This flexibility is a key feature of WordPress, making it suitable for a wide range of websites and content management needs.
Metadata examples and common usage
Some common examples of metadata in WordPress include:
- Post Metadata: This can include post tags, categories, featured images, publication date, author, and custom fields, among other data related to individual posts.
- User Metadata: As mentioned in a previous response, user metadata is data associated with user profiles, allowing for storing additional user information, preferences, and custom attributes.
- Term Metadata: This relates to data associated with taxonomy terms, such as categories and tags. It can be used to store additional information about these terms.
- Attachment Metadata: Metadata can be added to media files (attachments) to store information like captions, descriptions, and custom data associated with images, videos, and other media types.
- Custom Post Type Metadata: When you create custom post types, you can define and use custom metadata fields to store specific data relevant to those post types.
Let’s make our concept of metadata even more precise 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, 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, and activity. The amount of metadata available depends on the plugins you are using, but often plugins rely heavily on them.
What is WordPress user metadata?
WordPress user metadata is additional information or custom data associated with individual user profiles within a WordPress website. This metadata allows website administrators to store and manage specific details and attributes about users beyond the default user information provided by WordPress.
Some common use cases for user metadata in WordPress user management include:
- User Preferences: Storing user-specific preferences, such as notification settings, language preferences, or theme choices.
- Custom User Attributes: Adding extra fields to user profiles to store information like phone numbers, addresses, or user biographies.
- User Tracking: Recording and tracking user-specific data, such as the number of posts a user has authored, their last login date, or their membership level.
- Role-Based Data: Assigning specific data to users based on their user roles, which can be useful for user management and access control.
- Custom Functionality: Extending the capabilities of a WordPress website by associating user-specific data with custom features or plugins.
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 a 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 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 ); ?>
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 field type. 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 field 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 an 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 unsure 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 stored in the user meta is the standard text data type. This can be any kind of information stored in a text format. An example would be a person’s job title stored as a simple text. Here is an example where we search for all users whose job title is any kind of designer:
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 fulfill certain criteria.
Another field type that Users Insights supports is a date field type. This field type is read-only, 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 can sort the user table by this field in chronological order.
Here is an example where we have applied a filter to our custom date field to find all the users born after a selected date, with the user table sorted by this field in ascending order.
The dropdown field type allows selecting 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 ensure 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 also the options.
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 what 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 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 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 with 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 field 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 for searching custom fields and finding 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 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, 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 into the WordPress admin area of your site using custom fields and Users Insights.