How to Get All Taxonomy Terms for a WordPress Post

WordPress logo

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.

Use Case

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:

Taxonomy terms displayed as a comma-delimited string in a WordPress post

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.

  1. Edit your functions.php file.

  2. Paste the following code into functions.php:

  3. Call the function like this:

    $term_list = bsd_get_taxonomy_terms_as_string( 'bsd-specialty', 1035 );
    In the example above, bsd-specialty is the taxonomy slug and 1035 is the post ID.

Now, $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.”

Wrapping Up

Hopefully this post has not only shown you how to quickly implement a useful function, but also demonstrated some new use cases for taxonomies!


Related Articles

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
CONTACT US

Drop us a line

Schedule a complimentary consultation today.
0
Would love your thoughts, please comment.x
()
x