Home › Forums › General Issues › Help loop field repeat
hello I am using a loop in which I have to show a post that has a repeat type field, and I do not know why but only the first result of that field is showing me, it does not show me the rest of the elements ..
<?php
$args = array('posts_per_page' => '-1','post_type' => 'listas-compras','author' => $user_ID);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<?php
$posts = get_field('asignar_receta');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php if( have_rows('ingredientes')): // check for repeater fields ?>
<div class="texto-ingredientes">
<?php while ( have_rows('ingredientes')) : the_row(); // loop through the repeater fields ?>
<span class="cantidad-recetas"><?php $content = get_sub_field('cantidad'); ?></span>
<?php echo $content; ?>
<?php // set up post object
$post_object = get_sub_field('ingrediente');
if( $post_object ) :
$post = $post_object;
setup_postdata($post);
?>
<div class="ingredientes-recetas">
<?php the_title(); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<!-- End Repeater -->
<?php endif; ?>
Hi Dada,
I think it’s not working because each time you used setup_postdata() you probably kill the query loop called above.
Try this way (not functional, just to show you the logical path) :
$args = array('posts_per_page' => '-1','post_type' => 'listas-compras','author' => $user_ID);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post() ;
$listas_id= get_the_ID();
$asignar_recetas = get_field('asignar_receta', $listas_id);
if ($asignar_recetas) {
[...] // Write your html
foreach( $asignar_recetas as $receta) {
// each 'receta' is recetas post object
$receta_id = $receta->ID;
[...] // Write your html
echo '<a href="'.get_permalink($receta).'>">'.get_the_title($receta).'</a>'; // get post data with the post object
[...] // Write your html
if( have_rows('ingredientes', $receta_id)) {
while ( have_rows('ingredientes', $receta_id)) : the_row(); // get row with the receta_id to avoid mistake
[...] // Write your html
the_sub_field('cantidad');
[...] // Write your html
$ingrediente = get_sub_field('ingrediente'); // This is the ingrediente post object
[...] // Write your html
echo get_the_title($ingrediente);
[...] // Write your html
endwhile;
[...] // Write your html
}
[...] // Write your html
}
[...] // Write your html
}
[...] // Write your html
}
}
wp_reset_postdata();
Hi Dada
You probabely kill the query loop by using the setup_postdata(). Try this way (Not functional, just to show you the logical path) :
$args = array('posts_per_page' => '-1','post_type' => 'listas-compras','author' => $user_ID);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post() ;
$listas_id= get_the_ID();
$asignar_recetas = get_field('asignar_receta', $listas_id);
if ($asignar_recetas) {
[...] // Write your html
foreach( $asignar_recetas as $receta) {
// each 'receta' is recetas post object
$receta_id = $receta->ID;
[...] // Write your html
echo '<a href="'.get_permalink($receta).'>">'.get_the_title($receta).'</a>'; // get post data with the post object
[...] // Write your html
if( have_rows('ingredientes', $receta_id)) {
while ( have_rows('ingredientes', $receta_id)) : the_row(); // get row with the receta_id to avoid mistake
[...] // Write your html
the_sub_field('cantidad');
[...] // Write your html
$ingrediente = get_sub_field('ingrediente'); // This is the ingrediente post object
[...] // Write your html
echo get_the_title($ingrediente);
[...] // Write your html
endwhile;
[...] // Write your html
}
[...] // Write your html
}
[...] // Write your html
}
[...] // Write your html
}
}
wp_reset_postdata();
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.