0

I have a Custom post type "event" it has a repeater field "event_session" that has 4 subfields "session_start", "session_title", "session_description", "session_speaker" (a post object relationship with CPT Speaker.

I'm trying to create a page that will load the subfields. This is what i've tried:

<?php

    $args = array(
        'post_type' => 'event'
    );

    $the_query = new WP_Query( $args );
?>


<!-- WP_Query WordPress loop -->
<?php

// check if the repeater field has rows of data
if( have_rows('event_session') ):

    // loop through the rows of data
    while ( have_rows('event_session') ) : the_row();

        // display a sub field value
        the_sub_field('session_start');

    endwhile;

else :

    // no rows found

endif;

?>

The problem is that i cant pull the relational data from CPT speaker. How do i get the title and company name (fields from "speaker) into this page?

NEW CODE:

<?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>
<?php   

$eventspeakers = get_sub_field("session_speaker");
if ($eventspeakers && count($eventspeakers)>0)
{
    foreach ($eventspeakers as $eventspeaker)
    {
$speaker = get_post($eventspeaker);
echo'speaker is'.$speaker->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 results in pulling the speaker posts title, but repeats it many times. One event looks like 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

2

Simply use get_sub_field("session_speaker"); function to retrieve that data. As you've said this is a relational field, it will return an array of post objects according to the docs.

In order to get data from each speaker, you need to loop through the resulting array. Example:

$speakers = get_sub_field("session_speaker");
if ($speakers && count($speakers)>0)
{
    foreach ($speakers as $speaker)
    {
        echo $speaker->ID; //$speaker is a post object//
    }        
}
  • this didn't work correctly i think. I doesn't load the data at all as i posted like you state here. I know something is working though because if i change "echo $speaker->ID" to "echo $speaker" the page shows the array data from the related field to the session_speaker. – rudtek Aug 18 '15 at 19:43
  • @rudtek you should be able to figure out what the issue is by using var_dump($speaker); and print_r($speaker); inside the foreach loop to figure out what the item is and what properties it has. You can then figure out how to get various values. – jammypeach Aug 19 '15 at 13:23
  • Thanks fore getting back to me, this looks like the same thing as just using <pre> tags printing the array. I don't get the array value titles (post_id, post_type, etc) – rudtek Aug 19 '15 at 21:13
  • @rudtek sorry but I'm not sure what you mean. Can you post a sample of the output of the print_r call? also a print_r of $speakers. – jammypeach Aug 20 '15 at 9:23
  • Iupdated my code above. I was able to pull the post title, but the echo keeps repeating. I'm SO confused! – rudtek Aug 25 '15 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.