ACF - Retrieve custom taxonomy from a relationship field

I have a custom post type, called prognosis with 3 custom taxonomies, Country, Competition, and Sport, I've also added a few ACF custom fields. I have another custom post type called analysis with an ACF relationship field prognosis connected to the prognosis custom post type.

I want to be able to display certain taxonomy/ies the custom post type prognosis is connected to, through the relationship field that I have selected in the analysis custom post type. I tried debugging the prognosis relationship field but I don't see any taxonomies attached to it.

Topic advanced-custom-fields advanced-taxonomy-queries custom-taxonomy custom-field custom-post-types Wordpress

Category Web


I managed to solve my issue by looping through a get_the_terms() function. In my case, I've also created a shortcode.

function display_taxonomy() { 

$prognoses = get_field('prognosis');
ob_start();
if( $prognoses ):
foreach( $prognoses as $prognosis ):
    $terms = get_the_terms( $prognosis->ID , 'my_custom_taxonomy' );
    foreach ( $terms as $term ):
        echo $term->name;
    endforeach;
endforeach;
endif;
return ob_get_clean();
}

add_shortcode( 'my_custom_taxonomy_shortcode', 'display_taxonomy' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.