Support

Account

Home Forums Front-end Issues List terms which also have a certain relationship Reply To: List terms which also have a certain relationship

  • Hi @jakegonzales

    I believe it would be easier to just re-list and group the returned relationship value in an array like this:

    $result = array();
    
    $relationships = get_field("stockists_participating");
    
    foreach( $relationships as $thepost ) {
        
        $thepost_terms = get_the_terms($thepost->ID, 'countries');
        
        foreach( $thepost_terms as $thepost_term ) {
            
            // initialize if the term not listed yet
            if( !isset($result[$thepost_term->slug]) ) {
                $result[$thepost_term->slug] = array();
            }
            
            $result[$thepost_term->slug]['term'] = $thepost_term;
            $result[$thepost_term->slug]['value'][] = $thepost;
            
        }
        
    }
    
    print_r($result);

    With that code, you can loop trough the $result instead. Please take a look at the printed result to see the structure of the data.

    I hope this helps 🙂