Home Forums Front-end Issues Relations field inside loop not working


Relations field inside loop not working

  • Hi,

    I’m working on a custom page template, I’m trying to display a relationship post object field inside a loop. And it’s not working. I did a lot of research before posting on this forum. All the answers are agree on one thing, you need to use the wp_reset_postdata() function to reset the $post object. That<s what I’m doing but I can’t get the rest of my loop to work !!

    Here’s my code :

    ‘post_type’ => ‘film’,
    ‘order’ => ‘DESC’,
    ‘posts_per_page’ =>-1,
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) : $my_query->the_post();

    // ACF relationship

    $post_object = get_field(‘film_dvd’);
    if( $post_object ):
    $post = $post_object;
    setup_postdata( $post ); ?>
    <h3>“><?php the_title(); ?></h3>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    // Rest of the loop

    <a href=”” class=”old-link” rel=”bookmark” title=”Permanent Link to <?php
    the_title_attribute(); ?>”>
    <div class=”list-movie”>
    <div class=”info-one”>
    <span class=”list-date desktop”><?php echo get_the_date(); ?> – </span><h1>
    <?php the_title(); ?></h1>
    <h2 style=”margin-left:20px;”>
    <?php echo get_field(‘prenom_realisateur’).” “.get_field(‘nom_realisateur’); ?>

    endwhile; ?>

    If someone can help, it would be very appreciated !

    Thank you

  • I found a solution, I simply did not setup_postdata, so I don’t need to reset it 🙂

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

The topic ‘Relations field inside loop not working’ is closed to new replies.