I’ve worked out a solution to this. I hope this can help someone else.
First add the following two functions to your functions.php (or plugin) file:
function get_terms_by_post_type( $taxonomies, $post_types ) {
global $wpdb;
$query = $wpdb->prepare( "SELECT t.*, COUNT(*) from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_type IN('".join( "', '", $post_types )."') AND tt.taxonomy IN('".join( "', '", $taxonomies )."') GROUP BY t.term_id");
$results = $wpdb->get_results( $query );
return $results;
}
function show_post_type_terms($taxonomy, $posttype = null ) {
global $post;
if(!isset($posttype)) $posttype = get_post_type( $post->ID );
$terms = get_terms_by_post_type( array($taxonomy), array($posttype) );
echo '<ul>';
foreach($terms as $term) {
$output="<li><a href="".get_bloginfo('url')."https://wordpress.stackexchange.com/".$taxonomy."https://wordpress.stackexchange.com/".$term->slug.'/?post_type=".$posttype."">'.$term->name.'</a></li>';
echo $output;
}
echo '</ul>';
}
Now on your custom post type archive pages you can use <?php show_post_type_terms('country'); ?>
, changing ‘country’ for the taxonomy that you wish to return.
The links created have a query appended to the end to return only the post type that you are currently viewing. If you are on a ‘videos’ archive page it will link to a taxonomy archive showing only video posts.