Support

Account

Home Forums Front-end Issues Extracting Relational data from repeater fields

Solved

Extracting Relational data from repeater fields

  • I have a custom post type “event” that has a repeater field in it. One of the repeater sub fields is a relational post object field to another custom post type that i need pull data from a custom field.

    So here is what i have. Event CPT has a field called event_session which is a repeater field. One of the sub-fields is called “session_speaker” (which is a relational post object that is connected to my speaker custom postype.)

    When someone creates a row from the repeater field in my event page they will choose a speaker. On the front end i want to pull that speakers company name and twitter link (both custom fields: speaker_company and speaker_twitter.

    Here is what i tried:

    <?php
    
        $args = array(
            'post_type' => 'event'
        );
    
        $the_query = new WP_Query( $args );
    ?>
    
    <!-- WP_Query WordPress loop -->
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
        <?php if(get_field('event_session')): ?>
            <?php while(has_sub_field('event_session')): ?>
                    <strong><?php the_sub_field('session_start'); ?>  </strong><?php the_sub_field('session_title'); ?><br>
    <?php
    $speakers = get_sub_field("session_speaker");
    if ($speakers && count($speakers)>0)
    {
        foreach ($speakers as $speaker)
        {
            echo $speaker->ID; //$speaker is a post object//
        }        
    }
    ?>	
    
            <?php endwhile; ?>
        <?php endif; ?>
    
    <?php endwhile; else: ?>
    
        <!-- Displayed if no posts or pages are available -->
        There are no posts or pages here!
    
    <?php endif; ?>

    This shows nothing, but if i change the echo $speaker->ID to just echo $speaker, the array data does show up.

    I tried from above as well:

    <?php
    
        $args = array(
            'post_type' => 'event'
        );
    
        $the_query = new WP_Query( $args );
    ?>
    
    <!-- WP_Query WordPress loop -->
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
        <?php if(get_field('event_session')): ?>
            <?php while(has_sub_field('event_session')): ?>
                    <strong><?php the_sub_field('session_start'); ?>  </strong><?php the_sub_field('session_title'); ?><br>
    
    <?php	$postObjects = get_sub_field('session_speaker');
    
    if($postObjects){
    foreach($postObjects as $post){
    setup_postdata($post);
    the_title();
    }
    wp_reset_postdata();
    }?>
    	
    	<?php echo $postObject->$post_title; ?>
    
            <?php endwhile; ?>
        <?php endif; ?>
    
    <?php endwhile; else: ?>
    
        <!-- Displayed if no posts or pages are available -->
        There are no posts or pages here!
    
    <?php endif; ?>

    This shows the speaker title but even though I have 4 rows in my repeater field it will only show the first one.

  • any chance for assistance? I appreciate it!

  • I don’t know if this is effecting it, but this line needs to be changed

    Looking at the second code example you posted

    <!-- WP_Query WordPress loop -->
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    to

    <!-- WP_Query WordPress loop -->
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    Also remove this line, the titles should be output in the loop of $postObjects

    <?php echo $postObject->$post_title; ?>

  • Nothing changed. with those changes.

    I tried this too but it keeps repeating my “speakers is” over and over…not sure why?

    <?php
    
        $args = array(
            'post_type' => 'event'
        );
    
        $the_query = new WP_Query( $args );
    ?>
    
    <!-- WP_Query WordPress loop -->
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
        <?php if(get_field('event_session')): ?>
            <?php while(has_sub_field('event_session')): ?>
                    <strong><?php the_sub_field('session_start'); ?>  </strong><?php the_sub_field('session_title'); ?><br>
    <!-- Start of speaker list -->
                    <div class="speaker_list">
                    <?php
                    $sessionspeakers = get_sub_field("session_speaker");
                    ?>    
                    <?php
                    if ( $sessionspeakers ) { 
                        
                        foreach ( $sessionspeakers as $sessionspeaker ) :
    
                            $speaker = get_post($sessionspeaker);
                            $speakerlink = get_permalink( $speaker->ID );
                            $speakerurltext = get_post_meta($speaker->ID, 'speakerurltext', $single = true); 
                            echo 'speaker is'. $speaker->post_title ; 
                        endforeach; 
                        
                    } ?>
                    </div><!-- End of speaker list -->
            <?php endwhile; ?>
        <?php endif; ?>
    
    <?php endwhile; else: ?>
    
        <!-- Displayed if no posts or pages are available -->
        <p>There are no posts or pages here!</p>
    
    <?php endif; ?>

    The result, under each event is this:

    8:00AM Registration & Breakfast
    speaker isChristian Kochspeaker isspeaker isspeaker isspeaker isspeaker isspeaker is2015speaker isspeaker isspeaker isspeaker is2015speaker isspeaker is2015speaker is2015speaker isspeaker isspeaker is2015speaker is2015speaker isspeaker is2015speaker isspeaker is2015speaker is2015speaker is

    Why does it keep repeating?

  • What is this set to return $sessionspeakers = get_sub_field("session_speaker"); Post Objects or Post IDs?

  • I have a field group called event. In that field group i have a repeater field called event_session. One of the fields in that repeater section is called session_speaker. It is a post_object related to a field in a different field group called speakers.

    I’m trying to create an event item where a user can choose who will be speaking at certain times in the events. These time slots i’m calling sessions.

    Here is a link to the fields in my event field group:
    http://awesomescreenshot.com/05756lqh29

    SO the line you mentioned above is just setting a variable to that subfield so i can pull content from the speakers field group.

  • The first thing that I see is that you’re speaker field can only return a single speaker. Is that what you want or do you want to allow multiple speakers for each event_session. I see additional problems with your code, but I can’t really correct them without knowing this.

  • Hi John,
    I eventually do want there to be able to be more than 1 speaker in a session, i just couldn’t figure this part out to go to the next. I had thought to just make another repeater field called session_speaker2 or something, but if you know a better way, I would love to learn!

  • You can just change the option of that field to

    Select multiple values? to Yes

    The difference is that if that is set to No, then you’ll get a single post object, but if it’s set to Yes then you’ll get an array of post objects, and the loop for displaying them would be different.

    The following would replace the field loop if you set the field to allow multiple selections.

    
    <?php 
      if (get_field('event_session')) {
        while (has_sub_field('event_session')) {
          ?>
            <strong><?php the_sub_field('session_start'); ?></strong>
            <?php the_sub_field('session_title'); ?><br />
            <!-- Start of speaker list -->
            <div class="speaker_list">
              <?php
                $sessionspeakers = get_sub_field('session_speaker');
                if ($sessionspeakers) {
                  $count = 0;
                  echo 'Speakers: ';
                  foreach ($sessionspeakers as $speaker) {
                    $count++;
                    $speakerlink = get_permalink($speaker->ID);
                    
                    $speakerurltext = get_post_meta($speaker->ID, 'speakerurltext', true);
    
                    // if 'speakerurltext' is an acf field you could use
                    // $speakerurltext = get_field('speakerurltext', $speaker->ID);
                    
                    if ($count > 1) {
                      echo ', ';
                    }
                    echo $speaker->post_title;
                  } // end foreach speakers
                } // end if speakers
              ?>
            </div>
          <?php 
        } // end while has_sub_field
      } // end if get_field
    ?>
    

    If you’re only allowing one selection then you would not need a loop for the speakers.

  • THIS is BRILLIANT!!!! Better than I was thinking is possible. Tnank you VERY much.

  • ha, now i accidentally marked my OWN reply as the solution. JOHN did it though. HE solved this!

  • just out of curiosity though. what would be different if I only wanted there to be 1 selection? How would I do it with no loop?

  • You wouldn’t need the inner loop for the speakers.

    
    <div class="speaker_list">
    <?php
    	$speaker = get_sub_field('session_speaker');
    	if ($speaker) {
    		echo 'Speaker: ', $speaker->post_title;
    	} // end if speakers
    ?>
    </div>
    
  • John, you’ve been a great help. Just one last question and i believe this is done. how would i make it so that if there is more than 1 speaker it says “speakers” instead of just “speaker”? Can i count the array before the foreach?

    I tried inserting this:

    ...
                $sessionspeakers = get_sub_field('session_speaker');
                if ($sessionspeakers) {
                  $count = 0;
    	if($post_count=1){
    	echo 'Speaker '.$post_count.':<br>';
    	} else {
    	echo 'Speakers '.$post_count.':<br>';
    	}
    
                  foreach ($sessionspeakers as $speaker) {
    ...

    but it only returns 1 no matter how many speakers there are.

  • I would do something like this with the original code I posted

    
    <?php 
      if (get_field('event_session')) {
        while (has_sub_field('event_session')) {
          ?>
            <strong><?php the_sub_field('session_start'); ?></strong>
            <?php the_sub_field('session_title'); ?><br />
            <!-- Start of speaker list -->
            <div class="speaker_list">
              <?php
                $sessionspeakers = get_sub_field('session_speaker');
                if ($sessionspeakers) {
                  $count = 0;
                  echo 'Speaker';
                  if (count($sessionspeakers) > 1) {
                    echo 's';
                  }
                  echo ': ';
                  foreach ($sessionspeakers as $speaker) {
                    $count++;
                    $speakerlink = get_permalink($speaker->ID);
                    
                    $speakerurltext = get_post_meta($speaker->ID, 'speakerurltext', true);
    
                    // if 'speakerurltext' is an acf field you could use
                    // $speakerurltext = get_field('speakerurltext', $speaker->ID);
                    
                    if ($count > 1) {
                      echo ', ';
                    }
                    echo $speaker->post_title;
                  } // end foreach speakers
                } // end if speakers
              ?>
            </div>
          <?php 
        } // end while has_sub_field
      } // end if get_field
    ?>
    
  • Wow. Much simpler than I thought Thanks again!

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

The topic ‘Extracting Relational data from repeater fields’ is closed to new replies.