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