Support

Account

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

Solving

Relationship Field on single-cpt.php

  • I have two custom post types, collections & products. Each collection has a relationship field for associating products that belong to that collection.

    On the single-product.php template I am trying to list the other products that belong to the same collection as the single product.

    I’ve tried the ACF tutorial on bi-directional relationships but am not getting the objects from the relationship field at all. It is returning values related to the collection instead.

    My code:

    <div class=related-products>
    <h3>Products In This Collection</h3>
    
    <?php 
    
    $products = get_posts(array(
    'post_type' => 'scpl_collection',
    'meta_query' => array(
    array(
    'key' => 'products',
    'value' => '"' . get_the_ID() . '"',
    'compare' => 'LIKE'
    )
    )
    ));
    
    ?>
    
    <?php if( $products ): ?>
    
    <ul>
    
    <?php foreach( $products as $product ):
    
    $relatedImages = get_field('images', $product->ID);
    $size = 'related-product-thumbnail';
    $relatedThumb = $relatedImages['sizes'][ $size ];
    ?>
    
    <li>
    <a href="<?php the_permalink( $product->ID ); ?>" title="<?php the_title_attribute( $product->ID ); ?>">
    <span><img src="<?php echo $relatedThumb ?>"></span>
    <span class="related-product-title"><?php echo $product->post_title; ?></span></a>
    </li>
    					
    <?php endforeach; ?>
    </ul>
    
    <?php endif;
    // Reset Post Data
    wp_reset_postdata();
    ?>
    
    </div>
  • Hi @seasterling

    You need to get the collection first, then get the relationship field that is assigned to the collection like this:

    // Get the collections first
    $collections = get_posts(array(
        'post_type' => 'scpl_collection',
        'meta_query' => array(
            array(
                'key' => 'products',
                'value' => '"' . get_the_ID() . '"',
                'compare' => 'LIKE'
            )
        )
    ));
    
    // Loop through the collections
    foreach( $collections as $collection ){
        // Get the relationship value (products)
        $products = get_field('relationship_field_name', $collection->ID);
        
        // Loop through the products
        foreach( $products as $product ){
            // Do anything you want with the product
        }
    }

    I hope this helps ๐Ÿ™‚

  • 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>
    
    <?php 
    $collections = get_posts(array(
    'post_type' => 'scpl_collection',
    'meta_query' => array(
    array(
    'key' => 'products',
    'value' => '"' . get_the_ID() . '"',
    'compare' => 'LIKE'
    )
    )
    )); ?>
    
    <?php foreach( $collections as $collection ):
    
    $products = get_field('products', $collection->ID); ?>
    
    <ul>
    
    <?php foreach( $products as $product ):
    
    $relatedImages = get_field('images');
    $size = 'related-product-thumbnail';
    $relatedThumb = $relatedImages['sizes'][ $size ];
    ?>
    
    <li>
    <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>
    </li>
    
    <?php endforeach; ?>				
    </ul>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    </div>
  • This works. Now just need to eliminate the current product from the list.

    $relatedImages = get_field('images', $product->ID);
    $size = 'related-product-thumbnail';
    $relatedThumb = $relatedImages['sizes'][ $size ];
    ?>
    
    <li>
    <a href="<?php echo get_permalink( $product->ID ); ?>" title="<?php echo get_the_title( $product->ID ); ?>">
    <span><img src="<?php echo $relatedThumb ?>"></span>
    <span class="related-product-title"><?php echo get_the_title( $product->ID ); ?></span></a>
    </li>
  • Hi @seasterling

    You should be able to check if the current product loop is the same as the current product page like this:

    // Get the current product page ID
    $current_product_id = get_the_ID();
    
    foreach( $products as $product ):
    
        // Skip if the current product is listed
        if( $current_product_id == $product->ID ){
            continue;
        }
    
        $relatedImages = get_field('images', $product->ID);
        // Rest of the code here
        
    endforeach;

    I hope this helps ๐Ÿ™‚

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.