Support

Account

Home Forums Add-ons Repeater Field Repeater with Post Object

Solved

Repeater with Post Object

  • Hey there,

    I have a repeater field, inside it is a post object field. The post type is product and the “allow multiple values” is enabled.

    I have the following code to print the IDs of each product in the repeater field. In the back-end, I have 3 repeater rows thus far.

    <?php while(the_repeater_field('product_rows')) : ?>
    <?php $products = get_sub_field('select_products'); ?>
        <li>
            <?php if( $products ) : ?>
                <?php foreach( $products as $product): ?>
                    <?php setup_postdata( $product ); ?>
                    <?php the_sub_field('select_products', $product->ID); ?>
                    <?php wp_reset_postdata(); ?>
                <?php endforeach; ?>
            <?php endif; ?>
        </li>
    <?php endwhile; ?>

    The result I’m getting is:

    53817
    65470
    53815, 65491 53815, 65491

    For some reason, when I select more than one product, the IDs are printed twice. Any idea why?

    Kind regards
    Chris

  • Ended up needing way less code.

    The below is fixing the issue.

    <?php while(the_repeater_field('product_rows')) : ?>
    <?php $products = get_sub_field('select_products'); ?>
        <?php the_sub_field('select_products') ?>
    <?php endwhile; ?>

    Thanks
    Chris

  • For anyone out there. This was the end result, it works.

    Basically I’m generating a shortcode for each row in the back-end. And with an object field, you can pick your products.

    <?php while(the_repeater_field('product_rows')) : ?>
        <?php $products = get_sub_field('select_products') ?>
        <li>
            <?php echo do_shortcode("[products ids='".implode(',',$products)."']"); ?>
        </li>
    <?php endwhile; ?>

    Chris

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.