Support

Account

Home Forums General Issues Relationship Field Issue

Unread

Relationship Field Issue

  • Hello! I’m having the following problem. Right now lets say i have “Products” custom post type, and “Recipes” custom post type. In a single recipe post, i want to include the ingredients (products) that i used for the recipe. In advanced custom fields i’ve added a Relationship Field for Recipes, to choose posts from Products. I named the field “products_of_recipe”. I chose some products and now i want to display them inside the single recipe post. I’ve added the following code which i read in the documentation but it doesn’t work. What am i doing wrong?

    <?php 
    
            $posts = get_field('products_of_recipe');
    
            if( $posts ): ?>
            <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
              <?php setup_postdata($post); ?>
          <div class="col-2 no-margin-left-wbottom">
            <img src="<?php the_field('single_product_thumb'); ?>">
          </div>
              <?php endforeach; ?>
          <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
          <?php endif; ?>
Viewing 1 post (of 1 total)

The topic ‘Relationship Field Issue’ is closed to new replies.