Home › Forums › General Issues › Can't output repeated fields?
Hello,
I’m still quite new to using ACF, and part of me reaching out is to better understand ACF, so currently, I’m trying to output more than one “repeated field” I can var_dump and see that there are 2 arrays with different data, but visually the product data is only showing data from one of the two arrays, I feel like I’m missing and if or something somewhere, but I can’t figure out where.
$featuredProductSlide = get_field('featured_products_repeater');
$featuredBestSellerSlide = get_field('best_selling_products_repeater');
<div class="product_slider-1">
<?php if ($featuredProductSlide) : ?>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
<?php endif; ?>
</div>
<?php if ($featuredBestSellerSlide) : ?>
<div id="new_arrivals" class="product_slider-3">
<div class="product_slider_seven">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
<?php endif; ?>
</div>
You are checking to see if there is something in the repeater, but you are then not using anything in the repeater, instead you are doing a product query and using the results of that query.
It’s very hard to tell what you’re really trying to do from this code.
What types of fields are the sub fields of this repeater?
Hi John,
My apologies so, I’m attempting to display three different slides, each slide can display 3 different products. I’m currently not calling any subfields. I’m just calling the parent fields which are the following.
$featuredProductSlide = get_field('featured_products_repeater');
$featuredBestSellerSlide = get_field('best_selling_products_repeater');
Maybe there’s a resource link to help me understand this better? -I feel like I’m way off now. Ha!
But you are not using anything that is in those repeater parent fields. After you get the value and see if it contains anything you are simply ignoring anything that it might contain.
It would be really helpful to know what types of fields the sub fields are and what data they contain.
https://www.advancedcustomfields.com/resources/repeater/
Hey John,
Thanks for the link! -The subfields contains post object
if (have_rows('featured_products_repeater')) {
?>
<div class="product_slider-1">
<?php
while (have_rows('featured_products_repeater')) {
the_row();
// https://www.advancedcustomfields.com/resources/post-object/
$post = get_sub_field('post_object_field');
setup_post_data($post);
wc_get_template_part('content', 'product');
}
?>
</div>
<?php
wp_reset_postdata();
}
Hey, so this snippet was helpful but I’m still not getting any data output, the link includes foreach loop, is this implying that I need foreach loops within the while loops? -Kinda weird if it is no?
Since you are using a post object field inside of a repeater I assumed that each post object field would contain a single post.
But yes, if the post object field can have multiple posts then would be something like
$posts = get_sub_field('post_object_field');
foreach ($posts as $post) {
setup_post_data($post);
wc_get_template_part('content', 'product');
}
Sorry, I don’t know why I can’t wrap my head around this, I think I’ve just been stressed out or something. I’ve tried a few other variations myself with the foreach loop, and then now yours am I missing something here? What else should I check?
Here’s my full code I’m testing with
<?php
// Featured products feilds
$featuredProductTitle = get_field('featuredproducttitle');
$featuredProductSlide = get_field('featured_products_repeater');
$featuredBestSellerSlide = get_field('best_selling_products_repeater');
var_dump($featuredProductSlide);
?>
<br>
<h2>Data</h2>
<?php
var_dump($featuredBestSellerSlide);
?>
<!-- Slider Menu -->
<div class="product_slider_title_section">
<div class="slider_title">
<h2><?php echo $featuredProductTitle ?></h2>
<hr class="title_line">
</div>
<div class="product_slider_controls">
<h3><a id="featured">Featured</a></h3>
<h3><a id="best_selling">Best Seller</a></h3>
<h3><a id="new_arrivals">New Arrivals</a></h3>
</div>
</div>
<div id="featured">
<div class="product_slider-1">
<?php if(have_rows('featured_products_repeater')):?>
<?php while( have_rows('featured_products_repeater')): the_row(); ?>
<?php $args = array(
'post_type' => 'product',
'posts_per_page' => 3,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<?php endwhile;?>
</div>
<?php endif;?>
</div>
<div id="best_selling">
<?php if (have_rows('best_selling_products_repeater')) { ?>
<div class="product_slider-2">
<?php
while (have_rows('best_selling_products_repeater')) {
the_row();
$posts = get_sub_field('best_selling_product');
foreach ($posts as $post) {
setup_post_data($post);
wc_get_template_part('content', 'product');
}
}
?>
</div>
<?php wp_reset_postdata();
} ?>
</div>
<div id="new_arrivals" class="product_slider-3">
<div>TEST TEST</div>
<div>TEST TEST</div>
</div>
Also, I can 100% confirm that those are the correct field names.
The first <div class=”product_slider-1″> out puts correctly, however once I get <div class=”product_slider-2″> I’m changing it.
After doing some digging it seems like my issue is steaming from setup_post_data($post);
foreach ($posts as $post) {
var_dump($post);
// setup_post_data($post);
// wc_get_template_part('content', 'product');
}
Whenever I comment these in it breaks, I don’t think it likes the id data.
Like here’s the full code snippet
<div id="featured">
<?php if( have_rows('featured_product_repeater') ) { ?>
<div class="product_slider-1">
<?php
while (have_rows('featured_product_repeater')) {
the_row();
$posts = get_sub_field('featured_product_repeater');
foreach ($posts as $post) {
var_dump($post);
// setup_post_data($post);
// wc_get_template_part('content', 'product');
}
}
?>
</div>
<?php wp_reset_postdata(); } ?>
</div>
I don’t know.
In order for this template to show the product
wc_get_template_part('content', 'product');
it needs to know what post is being shown. WC does this by looking at the global $post, and the code I’ve given you set up the post as the global post.
Well, thanks for helping, I tried for quite a bit of time, still got nothing to work at all, I even adjusted how I declared the global $post.
This is where I last left off not too sure where to go from here, maybe it’s how I set up my ACF http://ussawssdev.wpengine.com/wp-content/uploads/2021/06/Capture.png
<div id="featured" class="product_slider-1">
<?php if( have_rows('featured_product_repeater') ) { ?>
<div class="testing_prod_div">
<?php
while (have_rows('featured_product_repeater')) {
the_row();
$posts = get_sub_field('product_2');
foreach ($posts as $post) {
var_dump($post);
setup_post_data($post);
wc_get_template_part('content', 'product');
}
}
?>
</div>
<?php wp_reset_postdata(); } ?>
<div>Testerrrrrrs</div>
</div>
Hey John,
Yeah, it does, it shows “int()” with the products id.
So that is actually the problem.
Check the return format for the post object field and make sure that it’s set to “Post Object” instead of ID.
Hey John,
I had to hop off this project for a little, but today I had time to get back to it, and I was finally able to wrap my head around it. Thanks for your help!
Firstly, I didn’t realize I could enter more than one value in the repeater input, I was duplicating the input and then entering a value, which wasn’t working because the input I was duplicating, obviously wasn’t being called in my code, but once I added more values to my repeater input I was calling in my code, I was able to output them.
Then, instead of setup_post_data($post);
I used wc_setup_product_data($post);
which didn’t break the entire site, like setup_post_data(); did.
Again, thanks for your help!
-Cheers
You must be logged in to reply to this topic.
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.