Support

Account

Home Forums General Issues Query Relationship within Relationship

Helping

Query Relationship within Relationship

  • Hi there,

    I’m having some trouble accessing data from a custom post type that is related another level deep. Basically, here’s the setup. I have a resources custom post type that has a relational field to a testimonials custom post type, and the testimonials custom post type has a relational field to a customers custom post type that includes the customer logo/name/link to their website etc.

    What I need to do is display all testimonials related to a resource, along with the logo, customer name, and URL.

    So basically: Resource <—- Testimonial <—- Customer

    I can pull the testimonial data just fine, but I can’t seem to figure out how to access the Customer data that is related to each testimonial.

    Here’s what I have so far on the single-resource.php WordPress page…

    <?php
      $relatedTestimonials = get_field('related_testimonials');
    
      //Check to see if there actually are related testimonials on resource custom post
      if($relatedTestimonials){
        foreach($relatedTestimonials as $testimonial){  
          //get relational field 'customer' (which should get returned as post object) on current 'testimonial' custom post
          $testimonialCustomer = get_field('customer', $testimonial->ID);
          if($testimonialCustomer){
            foreach($testimonialCustomer as $customer){
              //get customer logo img URL string from customer custom post
              echo '<img src="'.get_field('customer_logo', $customer->ID).' class="img-responsive center-block">';
            }
          } 
        }
      }
    ?>

    Any ideas on how I access the data in the customer custom post type? This doesn’t seem to be pulling anything.

    Thanks!

  • Are you still looking for help with this? if yes, a couple of questions.

    Is the customer field on the testimonial a Relationship field or a Post Object? Since I’m assuming that there can only be a single customer related to a testimonial it makes more sense to me that it would be a Post Object field which would return a single post object instead of an array.

    If this is not the case can you do a dump of $testimonialCustomer right after you get the field? Something like this would work`
    echo ‘

    '; print_r($testimonialCustomer); echo '

    ‘;

    then post the results.

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

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