4 March 2024

How to Create Your Own Custom Categories for WordPress Users

By Ronald Smith

Have you ever wanted to organize your WordPress content in a more personalized way? Well, you’re in luck! With the Custom Taxonomy feature, introduced in WordPress 2.9, you have the power to create custom groups for your Posts, Pages, and even your own Custom Post Types.

Let’s say you’re working on a fantastic website that features a directory of books. You’ve already created a special area for your book listings using a Custom Post Type called “Books.” Now, here’s where things get interesting: with Custom Taxonomy, you can go a step further and create your very own custom categories for these books. We’ll call them “Genres.”

Within your Genre taxonomy, you can get super creative and craft a whole range of unique categories, known as “terms,” to help you group your books better. For example, you could have terms like Fiction, Kids, or Biography. It’s like having your very own book classification system!

Unfortunately, at this point, we can’t register Custom Taxonomy to Users. It’s not as simple as registering it for other Post Types. But, I can see how this idea could be really useful. Instead of creating new User Roles, we could use it to add extra attributes to users, like their job, profession, or role within an organization. It would also let us search for users based on these attributes. So if this sounds like something that would benefit your website, here’s a tip to get you started.

To make things easier, we’ll use a plugin called User Taxonomies. Let’s begin by installing it.

Once you activate the plugin, you’ll need to go to GenerateWP to generate the Taxonomy codes. These codes need to be added to the functions.php file in your theme. Let me show you an example of the code snippet below. I’ve made it shorter for this article, but you can click on this link to see the full code.

If ( ! function_exists( ‘user_staff_position’ ) ) < function user_staff_position() < register_taxonomy( 'staff_position', 'post', $args ); >add_action( ‘init’, ‘user_staff_position’, 0 ); >

Now, in the following line, you need to change the Post Type parameter:

register_taxonomy( ‘staff_position’, ‘post’, $args );

Instead of using ‘post’, change it to ‘user’, like this:

register_taxonomy( ‘staff_position’, ‘user’, $args );

After making this change, go to the WP-Admin. You’ll notice a new menu added under the Users menu, as shown below.

How to Create Your Own Custom Categories for WordPress Users

Setting Up the Custom Taxonomy

First, let’s head over to the new menu and make some new categories. In this case, I’ve chosen to create two categories: CEO and Managers.

How to Create Your Own Custom Categories for WordPress Users

Alright, let’s do this. First, head over to the user editing screen. It’s where you can make changes to a user’s profile and settings.

Once you’re there, you need to assign one item from the taxonomy to the user. This means linking a specific category or classification to the user.

By doing this, you’re organizing the user into a specific group based on a shared characteristic or attribute.

It’s an important step in managing and categorizing users, ensuring that they are properly grouped and organized for administrative purposes.

So, go ahead and assign that item from the taxonomy to the user. It’s a simple task that will help keep everything organized and structured.

How to Create Your Own Custom Categories for WordPress Users

Asking the Users

Let’s showcase the users in our theme based on the given category (of the classification). But before we proceed, we need to create a fresh page template. I’ll guide you through adding the necessary codes to this new template.

In this specific scenario, we can’t fetch the users using methods like get_users or WP_User_Query. When you use the WP_User_Query class, it doesn’t include the Custom Taxonomy assigned to the users. However, Justin Tadlock has a solution for us. He suggests using the get_objects_in_term function instead.

This function gives us the ID of the objects (in our case, the object represents the user) that are associated with the given category. To use this function, we need two pieces of information: the ID of the category and the name of the taxonomy. You can find the category ID in the URL bar of your browser when you’re editing it, like this:

Hey there! So once you’ve got the ID, just pop it right into the function like this:

$users = get_objects_in_term(3, ‘user_position’);

If you want to see the IDs of the objects that have been retrieved, you can use var_dump(). In my case, it returned the users with the ID of 1 and 3.

By using these IDs, you can also get the user’s name and avatar, for example.

…and guess what? Here’s the result!

That’s all there is to it. Feel free to tweak the code above to fit your needs.