Support

Account

Home Forums General Issues Using relationship field to display repeater from a custom post type

Solved

Using relationship field to display repeater from a custom post type

  • So, I’m trying to display a repeater from a custom post type depending on which relationship fields have been selected.

    For some background, I’m using this to show a certain region’s accommodation on an Events custom post type. Which should work like the following;
    The client creates a custom post (The town) with a repeater (The accommodations) inside with Name, Location, etc. Once they have created the town, they then pick that from the list on Event custom post type.

    I can get it as close as displaying the fields from two locations but im unsure how to make it show based on what towns are selected in the relationship field.

    <div class="event-accordian">
    
                <div class="event-container">
                    <h3 class="accordian">
                        <span>
                            Accommodation<i class="fas fa-chevron-down"></i>
                        </span>
                    </h3>
                    <div class="content" style="display:none">
    
                        <div class="row">
    
                            <?php
                            $args = array( 'post_type' => 'accommodation', 'posts_per_page' => -1 );
                            $loop = new WP_Query( $args );
                            while ( $loop->have_posts() ) : $loop->the_post();
                            ?>
    
                            <?php
                            if( have_rows('acommodation_location_repeater') ):
                            while ( have_rows('acommodation_location_repeater') ) : the_row();
                            ?>
    
                            <div class="col-md-3">
                                <?php the_sub_field( 'accommodation_name' ); ?>
                                <?php the_sub_field( 'accommodation_description' ); ?>
                                <?php the_sub_field( 'accommodation_address' ); ?>
                                <?php the_sub_field( 'accommodation_phone' ); ?>
                                <?php the_sub_field( 'accommodation_email' ); ?>
                                <?php $accommodation_website = get_sub_field( 'accommodation_website' ); ?>
                                <?php if ( $accommodation_website ) { ?>
                                <a href="<?php echo $accommodation_website; ?>"><?php echo $accommodation_website; ?></a>
                                <?php } ?>
                            </div>
    
                            <?php endwhile; else : endif; ?>
    
                            <?php endwhile; wp_reset_query(); ?>
    
                        </div>
    
                    </div>
                </div>
            </div> 

    The issue begins when I try to specify the relationship first:

    <?php
                            $accomodation = get_field('accommodation_relationship');
                            if( $accomodation ): ?> 

    I feel like I’m close but might have the order wrong!

  • To show the accommodations (repeater) that is on the post type town when showing events you need to get the location fields and then supply the post ID to get the repeater from.

    if (have_fields('your-repeater', $post_id)) {
      while(have_rows('your-repeater', $post_id)) {
        the_row();
        get_sub_field(.... etc)
    
  • Thanks John, I ended up solving it, my working code ended up being:

    <?php $accommodation_relationship = get_field( 'accommodation_relationship' ); ?>
    <?php if ( $accommodation_relationship ): ?>
    <?php foreach ( $accommodation_relationship as $post ):  ?>
    <?php setup_postdata ( $post ); ?>
    
    <?php
    if( have_rows('acommodation_location_repeater') ):
    while ( have_rows('acommodation_location_repeater') ) : the_row();
    ?>
    
    <div class="col-md-3 accommodation">
    <h3><?php the_sub_field( 'accommodation_name' ); ?></h3>
    <p class="description"><?php the_sub_field( 'accommodation_description' ); ?></p>
    <p class="address"><?php the_sub_field( 'accommodation_address' ); ?></p><br />
    <a href="tel:<?php the_sub_field( 'accommodation_phone' ); ?>" class="contact-link"><?php the_sub_field( 'accommodation_phone' ); ?></a><br />
    <a href="mailto:<?php the_sub_field( 'accommodation_email' ); ?>" class="contact-link"><?php the_sub_field( 'accommodation_email' ); ?></a>
    <?php $accommodation_website = get_sub_field( 'accommodation_website' ); ?>
    <?php if ( $accommodation_website ) { ?>
    <br />
    <button class="accommodation-bttn"><a href="<?php echo $accommodation_website; ?>">Website <i class="fas fa-chevron-right"></i></a></button>
    <?php } ?>
    </div>
    
    <?php endwhile; else : endif; ?>
    
    <?php wp_reset_query(); ?>
    
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    Thanks!

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

You must be logged in to reply to this topic.