How to Register Custom Taxonomy For WordPress Users – Netadroit WebDesign
The Custom Taxonomy characteristic has been launched since WordPress 2.9. It permits you to create customized teams for Post, Page in addition to Custom Post Types.
Say that you’re constructing a e-book listing website, and you’ve got created a Custom Post Type for posting the Books. By utilizing Custom Taxonomy, you’ll be able to create a customized taxonomy for it, known as Genre. Within this Genre taxonomy, you’ll be able to create various gadgets (which technically is named phrases) equivalent to Fiction, Kids, or Biography for grouping the Books.
Unfortunately, at this level, we are able to’t register Custom Taxonomy to Users; not less than not in an easy manner as we might register it within the different Post Types. One good software that we might foresee from this concept is that we are able to use it to assign further consumer attributes, equivalent to their occupation, career or organizational place, instead of registering a brand new set of User Roles. It additionally opens the likelihood to question the customers based mostly upon the assigned taxonomy phrases.
If this concept is one thing that will profit your website, check out this tip.
Recommended Reading: How To Display List Of Authors With Pagination
Getting Started
First, we are going to set up a plugin named User Taxonomies to simplify our job.
Once the plugin is activated. Go to GenerateWP to generate the Taxonomy codes. Put the code output within the features.php file of your theme. This code snippet beneath is an instance. Though, it has been stripped out to make this text look shorter. You can comply with this hyperlink to see the total code.
if ( ! function_exists( 'user_staff_position' ) ) { perform user_staff_position() { register_taxonomy( 'staff_position', 'put up', $args ); } add_action( 'init', 'user_staff_position', 0 ); }
Now, change the Post Type parameter within the following line:
register_taxonomy( 'staff_position', 'put up', $args );
…from put up
to consumer
, like so:
register_taxonomy( 'staff_position', 'consumer', $args );
Now, go to the WP-Admin, and it is best to discover a new menu added below the Users menu, as seen beneath.

Assigning the Custom Taxonomy
Navigate to the brand new menu and create a couple of phrases. For this instance, we created two gadgets: CEO and Managers.

Then go to consumer modifying display screen and assign one merchandise from the taxonomy to the consumer.

Query the Users
We are going to show the customers within the theme based mostly on the given time period (of the taxonomy). But earlier than going additional, let’s create a brand new web page template. We are going add the codes all through the next part inside this new template.
In this specific case, we gained’t have the option to question the customers with get_users
or WP_User_Query
; once you create a brand new WP_User_Query
class, it doesn’t output the Custom Taxonomy that’s assigned to the customers. Justin Tadlock, in his tutorial, exhibits us how to use the get_objects_in_term
perform, as an alternative.
This perform outputs the thing ID (which in our case the thing means the consumer) which are tied with the time period. To use it, we want two parameters: the Term ID and the Taxonomy identify. You can spot the Term ID on the Browser URL bar once you edit it as proven beneath.

Once you’ve discovered the ID, put it inside the perform, like so.
$customers = get_objects_in_term(3, 'user_position');
You can use var_dump()
to show the thing IDs which have been retrieved; In my case, it returns the customers with the ID of 1
and 3
.

Using these IDs, we are able to additionally retrieve, for instance, the consumer identify and avatar.
<ul> <?php if ( !empty( $customers ) ) : ?> <?php foreach ( $customers as $id ) : ?> <li class="user-entry"> <determine><?php echo get_avatar( get_the_author_meta('electronic mail', $id), '40' ); ?></determine> <h5 class="user-title"><a href="https://www.hongkiat.com/<?php echo esc_url( get_author_posts_url( $id ) ); ?>"><?php the_author_meta( 'display_name', $id ); ?></a></h5> </li> <?php endforeach; ?> <?php endif; ?> </ul>
…and, lastly, right here is the consequence.

That’s it. You can freely modify the above codes to meet your requirement.