Support

Account

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

  • 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 🙂