Last week, I needed to get all of the taxonomy terms assigned to a post, returning them as a comma-delimited string. In this short article, I’ll provide the use case from an actual client, then the custom function used to return those terms in the format they needed. In addition, I’ll explain the purpose behind the use case, and how it helped our client.
A taxonomy named “Specialty” has 11 terms. Some of those terms are assigned to a post serving as a user profile, indicating her areas of expertise. Our client needs to get a list of all taxonomy terms assigned to this user so they will appear below her name in the post. For example, consider this public profile from Franklin Street, one of our clients:
In the example above, the terms “landlord representation,” “real estate,” and “tenant representation” are all assigned to Abby. Visitors to this page can see at a glance Abby’s areas of expertise. Next, I’ll show you the actual function.
Custom Function Returning Taxonomy Terms
Time needed: 5 minutes.
Here’s how to achieve the same result on your site.
- Edit your functions.php file.
- Paste the following code into functions.php:
- Call the function like this:
= bsd_get_taxonomy_terms_as_string( 'bsd-specialty
', 1035 );
In the example above,
bsd-specialtyis the taxonomy slug and
1035is the post ID.
$variable contains a list of all terms for this particular post and taxonomy, separated by commas.
Why Taxonomy Terms?
Now, why not just type Abby’s specialties directly into the post?
Using taxonomies and terms this way is a key feature of the BSD Team and User Profiles plugin. You may already know that you can assign terms to posts. One of the cool things about assigning terms to a user profile you create with this plugin is that you can then display articles specifically related to their area of expertise.
Scrolling further down Abby’s profile, we see that the related articles shortcode has displayed three blog posts. Each of these posts also has a taxonomy of “specialty,” and the terms assigned to each include “landlord representation” and “real estate.”
Hopefully this post has not only shown you how to quickly implement a useful function, but also demonstrated some new use cases for taxonomies!