ACF: Get Post Object data within a Repeater

I absolutely love Advanced Custom Fields. It’s part of the reason I love using WordPress so much. It makes things very easy for clients, and is a breeze to develop.

Every now and again I struggle to with grabbing and displaying desired data. A recently project required extracting data from a Post Object, within a Repeater. I struggled for at least an hour with tips from Stack Overflow as well as ACF forums without much luck.

Eventually I turned back to the original Post Object documentation for the answer. Basically, you’ll want to grab the Post Object data, then wrap the whole thing in your repeater code.

Here’s how I did it, using Flexslider. My repeater field is slides and the Post Object field is featured_projects:

<section class="slider">
 
    <?php if( have_rows('slides') ): ?>
 
        <div id="slider" class="flexslider">
 
            <ul class="slides">
 
            <?php while ( have_rows('slides') ) : the_row(); ?>   
 
                <li>
 
                    <?php $post_object = get_sub_field('featured_projects'); ?>
 
                    <?php if( $post_object ): ?>
 
                        <?php $post = $post_object; setup_postdata( $post ); ?>
 
                        <a href="<?php the_permalink(); ?>"><img src="<?php the_field('featured_image'); ?>" alt="<?php the_title(); ?>" /></a>
 
                        <?php wp_reset_postdata(); ?>
 
                    <?php endif; ?>
 
                </li>
 
            <?php endwhile; ?>
 
            </ul>
 
        </div>
 
    <?php endif; ?>
 
</section>

18 Comments

  1. Justin on December 1, 2014 at 8:49 pm

    Thank you!!!

    • Bryan Hoffman on May 6, 2015 at 3:21 pm

      My pleasure. I come back to this one from time to time. Today included.

  2. Aksam Zarook on June 26, 2015 at 5:38 am

    Bryan,
    Thanks. Just curious to see some live pages with advance layouts you\’ve made using acf 🙂

    • Bryan Hoffman on June 26, 2015 at 9:32 am

      Thanks for the link from your post Aksam. I\’ve been using ACF on nearly every project for a couple of years now. We tend not to get too complex – I never used the Flexible Content field for example. But it\’s times like this project posted here that minor complex items come up.

      • Aksam Zarook on June 27, 2015 at 4:18 am

        Thanks. I didn\’t know about Flexible content field. Looks like its a page builder like plugin. good to know options like these exist. You never know when you will need them.

        I came across this repeater field issue when i was developing the custom post for this page design
        http://colombotraders.com/demo/infinity/draft2/tour-details-page.html

        What do you think about this design? I\’ve not developed a post type this advance before, so I\’m wondering how it will work in a live environment.

        So far, I have already developed the custom post page except for the slider. It\’s working nicely on my local wamp server. The custom post type has about 10 custom fields including a textarea to save google my map code, another to save slider code, a post object field, few other normal fields and 3 repeater fields each with several sub fields.

      • Bryan Hoffman on June 29, 2015 at 9:40 am

        Looks like a fun layout to build. Custom post types and custom taxonomies!

  3. […] similar threads on StackOverflow etc., I finally stumbled on Bryan Hoffman’s blog post “ACF: Get Post Object data within a Repeater” which showed me the error in my coding.  It’s simple and I think the ACF […]

  4. Peter Gramstrup on July 7, 2015 at 5:59 am

    Thank you very much!! Worked perfect. 🙂

  5. jas on February 4, 2016 at 1:05 pm

    might want to close your #slider div?

  6. Steve on March 18, 2016 at 6:08 pm

    Hello,
    i have post object called project_for_apartment. Then i have repeater called facility_fac. Inside repetear i have subfield called feature_value. I cant manage for days to extract those values on my custom template.

    • Bryan Hoffman on March 21, 2016 at 10:25 am

      Did you get this sorted out Steve? Go ahead and post your code and we\’ll see if we can sort it out.

  7. Douglas Ward on June 18, 2018 at 5:47 pm

    Years later, but still saving the day. Thank you!

    • Bryan Hoffman on June 19, 2018 at 8:53 am

      I’m glad to know this still works and you found it useful Douglas!

  8. andrew on July 17, 2018 at 4:54 pm

    Hey thanks for the tip! Unfortunately I’m only getting one of my repeater values printed. Narrowed it down to the while loop failing after the first post prints. Any ideas why that may be?

    If I kill the and just enter random characters they will print the number of times that the while loop executes. However, with the post object assignment in there it will only loop once.

    • Bryan Hoffman on July 18, 2018 at 9:11 am

      Hi Andrew. If you could post your code somewhere I might be able to troubleshoot.

  9. Tom Walsh on February 25, 2019 at 5:39 am

    YES! This was perfect for what i needed! thank you very much!

Leave a Comment