WordPress Taxonomies

Taxonomies are a very powerful but less understood and less used feature of WordPress.

In this post, I’ll gather different topics related to WordPress taxonomies, solving issues and solutions to different situations.

How to get value of specific custom taxonomy for current post

To get the value of specific taxonomy for current post, we’ll use this function (and its related ones):

wp_get_post_terms( );

However how exactly to use it?

Well in your WordPress functions.php, add this code to see the output of the terms to get an idea how to get your term:

function get_current_post_taxonomies(){
 // declaring global $post object
 global $post;

 // getting all the taxonomies for current object, i.e. our post
 // these are the custom taxonomies we have probably
 $taxonomy_names = get_object_taxonomies( $post );
 print_r( $taxonomy_names );

 // getting any taxonomy objects for this current object of ours, these are different from the above code (detail needed)
 $taxonomy_objects = get_object_taxonomies( 'post', 'objects' );
 print_r( $taxonomy_objects);

 // e.g. if we had a custom taxonomy tag, priority, setup and we want to get its values, we use this
 $getsomething = get_terms('priority');
 print_r($getsomething);

 // e.g. if we want to get the value(s) of this priority tag for our current post only, we'll use this
 $terms = wp_get_post_terms( $post->ID, 'priority' );

 // you may notice that the above one outputs an array of objects, so if we had to get only one saved item in it, we can use this:
 print_r($terms[0]->name);

}
add_action('wp_head','get_current_post_taxonomies');

Hope this helps. If you need any assistance do let me know!

Leave a Reply

Your email address will not be published.