3
votes

I have a custom taxonomy (tax_classes) that has been registered to two custom post types (cpt_events and cpt_galleries). On the 'index' page for each of the custom post types I want to get the terms from the tax_classes taxonomy for that specific CPT. For example on the events CPT I want to show the terms that have been used by its posts post only.

I have looked into get_terms('tax_classes'); but this gives me ALL the terms for the taxonomy. Unfortunately there isn't a 'post_type' argument for the get_terms WP function to help with the filtering.

Any help on this would be greatly appreciated

Thanks

J

2

2 Answers

0
votes

<?php $desc = wp_get_object_terms( $post->ID, 'your-taxonomy' ); if ( ! empty( $desc ) ) { if ( ! is_wp_error( $desc ) ) { foreach( $desc as $term ) { echo $term->name; } } } ?>

$post->ID knows the custom-post-type of the page you are on. The above code will look for your-taxonomy and echo it out.

0
votes

Have you looked at get_object_taxonomies(); ?

https://codex.wordpress.org/Function_Reference/get_object_taxonomies

Its first parameter is the custom post type and the second the taxonomy.