Home › Forums › Front-end Issues › Extracting Post Object data from within a Repeater › Reply To: Extracting Post Object data from within a Repeater
i know this is an older post but I can’t seem to get this worked out.
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 has a field called event_session which is a repeater field. One of the sub-fields is called “session_speaker” it is a 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).
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 -->
<p>There are no posts or pages here!</p>
<?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 -->
<p>There are no posts or pages here!</p>
<?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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.