Home Forums General Issues Relationship Field on single-cpt.php Reply To: Relationship Field on single-cpt.php

  • Thanks James. That almost works. The loop through the relationship field is looping the correct number of times, but it is returning the same product every time (the same product loaded by the single-scpl_product.php template). I actually want to remove that product from the loop.

    <div class=related-products>
    <h3>Products In This Collection</h3>
    $collections = get_posts(array(
    'post_type' => 'scpl_collection',
    'meta_query' => array(
    'key' => 'products',
    'value' => '"' . get_the_ID() . '"',
    'compare' => 'LIKE'
    )); ?>
    <?php foreach( $collections as $collection ):
    $products = get_field('products', $collection->ID); ?>
    <?php foreach( $products as $product ):
    $relatedImages = get_field('images');
    $size = 'related-product-thumbnail';
    $relatedThumb = $relatedImages['sizes'][ $size ];
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <span><img src="<?php echo $relatedThumb ?>"></span>
    <span class="related-product-title"><?php the_title(); ?></span></a>
    <?php endforeach; ?>				
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>