Title: Relationship within loop
Last modified: August 22, 2016

---

# Relationship within loop

 *  Resolved [kapppa](https://wordpress.org/support/users/kapppa/)
 * (@kapppa)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/relationship-within-loop/)
 * Hi there,
 * i´m trying to get a relationship field within a loop.
 * I created a loop called “hotel” and a field “hotel_text” and “hotel_location”.
 * the “hotel_location” is the relationship-field.
 * Following code is working if the relationship-field is not within a loop.
    — 
   <?php $values = CFS()->get(‘hotel_location’); foreach ($values as $post_id) {
   $the_post = get_post($post_id); echo $the_post->post_title; } ?> —
 * What code can i use to get it working within the loop?
 * I tried the code below, but it just throws out array array.
 * <?php
    $fields = CFS()->get(‘hotel’); foreach ($fields as $field) { echo $field[‘
   hotel_text’]; echo $field[‘hotel_location’]; } ?>
 * Thanks a lot
 * [https://wordpress.org/plugins/custom-field-suite/](https://wordpress.org/plugins/custom-field-suite/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [kapppa](https://wordpress.org/support/users/kapppa/)
 * (@kapppa)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/relationship-within-loop/#post-5343432)
 * Ok, found the solution on the website: [http://customfieldsuite.com/forums/questions/811/how-to-display-a-relationship-field-inside-a-loop](http://customfieldsuite.com/forums/questions/811/how-to-display-a-relationship-field-inside-a-loop)
 *     ```
       <?php
       // Get the loop fields (returns an associative array)
       $loop = $cfs->get('my_loop');
   
       // Iterate through the fields
       foreach ($loop as $field) {
           // returns a date string
           $my_date = $field['my_date']; // this returns a date string
   
           // returns an array of post IDs, which we need to iterate through
           $my_relationship = $field['my_relationship'];
   
           // output the  formatted date (google "PHP date")
           echo '<div>Date: ' . date('F j, Y', strtotime($my_date)) . '</div>';
   
           // output the relationship values
           echo '<div>Relationship</div>';
           foreach ($my_relationship as $post_id) {
               // Pass the post ID into get_the_title to get the post's title
               echo '<div>' . get_the_title($post_id) . '</div>';
           }
       }
       ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Relationship within loop’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-field-suite.svg)
 * [Custom Field Suite](https://wordpress.org/plugins/custom-field-suite/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-field-suite/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-field-suite/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-field-suite/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-field-suite/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [kapppa](https://wordpress.org/support/users/kapppa/)
 * Last activity: [11 years, 8 months ago](https://wordpress.org/support/topic/relationship-within-loop/#post-5343432)
 * Status: resolved