Home Forums ACF PRO Using relationship reverse query in a custom loop Reply To: Using relationship reverse query in a custom loop

  • I’m guessing here because I cannot see how the loops over the posts are done. You are probably having an issue do to nested loops. There is the first loop “The Loop” which is the main loop done by WP, then you have a second loop over the “instructors” nested in this loop and finally you have a “trip” loop nested in that loop. Generally at the end of a nested query loop a the function wp_reset_postdata() is called to reset the post to the previous loop, but when working with nested loops this breaks the previous loop. The reason for this is the wp_reset_postdata() always resets the post to the primary WP loop and not any intermediate loops.

    // primary WP loop
    while (have_posts) {
      // secondary loop
      while ($secondary_query->have_posts()) {
        // tertiary loop
        while ($tertiary_query->have_posts()) {
        // this call will not reset post data to post of $secondary query
        // this function always resets post data to primary loop post
        // in this case instead of this function you must call
      wp_reset_postdata(); // resets to primary loop post