Restore Woocommerce Loop after ACF Loop inside


I have an ACF Relationship field (post Object) called ‘variants’ assigned to Products post type. I’m including a list of thumbnails of the selected products from this field inside the product loop. It works fine on archive pages but it messes up on other loops such us related products or shortcodes. It basically gets the current post title for all looped products instead their own selves.

Any idea how to fix the loop inside the loop?
I tried wc_reset_loop(); and some other query resets but it doesn’t get back to the product query.
I tried with ACF ID instead of Object and new_name query instead of foreach with no result.

if (get_field( 'variants' )) {
    $posts = get_field('variants' );
    if( $posts ):  foreach( $posts as $post): setup_postdata($post); 
         $post_slug = $post->post_name;
         $clase = $post_slug . ' variant-img';
         the_post_thumbnail('shop_catalog', array('class' => $clase));
endforeach;  wp_reset_postdata();  endif;  }
Juarez 2 years 2020-12-21T07:10:42-05:00 0 Answers 4 views 0

Leave an answer