Support

Account

Home Forums General Issues Multiple relationship fields on same page not working

Solved

Multiple relationship fields on same page not working

  • Good evening!

    I have been wrestling with a little issue for a while so thought I’d try find some help on the official forums!

    I have two relationship fields on my homepage, one for posts and one for portfolio items. The first, get_field(‘posts’), gets the linked posts and displays them no problem.

    I then copied the same code and swapped ‘posts’ for ‘portfolio’ and it returns nothing. However if I comment out all of the get_field(‘posts’) code then suddenly the get_field(‘portfolio’) code returns back all of my portfolio items.

    I did some searching and found a previous post on these forums without much of a solution.

    Has anyone encountered this problem before or know why it might be occurring?

    Thanks!

    $posts = get_field('posts');
    if ($posts): foreach ($posts as $post): 
        // loops through posts as expected
    endforeach; endif;
    
    $portfolio_items = get_field('portfolio');
    if ($portfolio_items): foreach ($portfolio_items as $item): 
        // returns no portfolio items
    endforeach; endif;
  • What does the rest of the code do? Are you using setup_postdata()? If you are are you calling wp_reset_postdata() at the end of the first loop?

  • Hi John,

    The rest of the code is just layout code and data manipulation.

    However I didn’t think to use setup_postdata() and wp_reset_postdata() for a relationship field loop. I placed these functions at the start and end of my foreach loop and now it works! What a simple solution, proves I have much to learn still.

    Thanks a lot for the hint!

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

The topic ‘Multiple relationship fields on same page not working’ is closed to new replies.