WordPress: Get all terms for a post

Get all terms for a Post in WordPressFirst you need to get the available taxonomies so that you can pass them to the `wp_get_object_terms` function to get all of the terms / categories / tags / custom tax terms associated with a single post:

$taxonomies = get_taxonomies(”,’names’);
print_r($taxonomies); //optional for debugging

That will likely return something like this:

Array
(
[category] => category
[post_tag] => post_tag
[nav_menu] => nav_menu
[link_category] => link_category
[post_format] => post_format
[product_type] => product_type
[product_cat] => product_cat
[product_tag] => product_tag
[product_shipping_class] => product_shipping_class
[shop_order_status] => shop_order_status
)

Next you can use the `wp_get_object_terms` function, passing the post’s ID(s) and the list of taxonomies to get all of the terms associated:

global $post; //optional if you are inside the “loop”
$terms = wp_get_object_terms($post->ID,$taxonomies);
print_r($terms); //optional for debugging

Array
(
[0] => stdClass Object
(
[term_id] => 2
[name] => Blogroll
[slug] => blogroll
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => link_category
[description] =>
[parent] => 0
[count] => 7
)

[1] => stdClass Object
(
[term_id] => 14
[name] => Lightweight
[slug] => lightweight
[term_group] => 0
[term_taxonomy_id] => 14
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 1
)

[2] => stdClass Object
(
[term_id] => 15
[name] => tag1
[slug] => tag1
[term_group] => 0
[term_taxonomy_id] => 15
[taxonomy] => product_tag
[description] =>
[parent] => 0
[count] => 1
)

[3] => stdClass Object
(
[term_id] => 5
[name] => simple
[slug] => simple
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => product_type
[description] =>
[parent] => 0
[count] => 1
)

)

And that’s how you get all terms for a post in wordpress. I decided to write this after unsuccessfully searching for the terms below. Leave a comment if this helped you or if you have a fix / improvement / question.

  • wordpress get all terms post
  • wordpress get all taxonomies
  • wordpress get all terms
  • wordpress get all terms post

Related Posts:

  • No Related Posts
This entry was posted in Web Development and tagged , , , . Bookmark the permalink.

One Response to WordPress: Get all terms for a post

Leave a Reply

Your email address will not be published. Required fields are marked *