Support

Account

Home Forums Front-end Issues Accessing Relationship Field Values Within Relationship

Solved

Accessing Relationship Field Values Within Relationship

  • I have 3 custom post types:

    Transaction
    Producer
    Location

    Transaction contains a relationship field filtered by Producer, and Producer contains a relationship field filtered by Location.

    Transaction => Producer => Location

    On the Single Transaction page, I would like to output values from all 3 custom post types.

    I’m having no problem outputting values from the the Producer because of the direct relationship to the Transaction, but since the Location is a relationship to the Producer, I’m having trouble getting those values to come through.

    Currently, I’m trying a nested foreach loop, with the internal loop referencing a reverse query, but the value outputs incorrectly. Am I going about this the right way? Or is there a better way to access those values?

    Here is my code below on the Single Transaction Page:

    
    <h4>Transaction Contacts</h4>
    <?php
      $producers = get_field('transaction_contacts');
      if($producers):
    ?>
    <ul class="list-unstyled">
    <?php foreach( $producers as $p) : ?>
      <?php 
        $office_location = get_posts(array(
          'post_type' => 'producer',
          'meta_query' => array(
            'key' => 'office_location',
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
          )
        ));
      ?>
      <li>
        <p><?php echo get_the_title($p->ID); ?></p>
        <?php foreach( $office_location as $o) : ?>
          <p><?php echo get_the_title($o->ID); ?></p>
        <?php endforeach; ?>
      </li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    

    Thanks in advance for any help. I apologize if this has been answered before, I had trouble finding anything relating to it.

  • Have you tried just getting the “location” field when looping through the “Producers”?

    
    <h4>Transaction Contacts</h4>
    <?php
      $producers = get_field('transaction_contacts');
      if($producers):
    ?>
    <ul class="list-unstyled">
    <?php foreach( $producers as $p) : ?>
      <?php 
        $office_location = get_field('office_locations', $p->ID);
      ?>
      <li>
        <p><?php echo get_the_title($p->ID); ?></p>
        <?php foreach( $office_location as $o) : ?>
          <p><?php echo get_the_title($o->ID); ?></p>
        <?php endforeach; ?>
      </li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    
  • Perfect, that solved it! I think I had tried that before, but the important part I was missing was referencing the producer’s ID.

    I had tried this:
    $office_location = get_field('office_location');

    When all I needed was to refer to the $p->ID within the loop like you suggested:

    $office_location = get_field('office_location', $p->ID);

    Thanks very much!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Accessing Relationship Field Values Within Relationship’ is closed to new replies.