Support

Account

Home Forums General Issues Relationship query from custom post type on external page Reply To: Relationship query from custom post type on external page

  • I’ve boiled down your code to the important bits so that it’s easier to see why it’s not working and what to change

    
    <?php 
      // start of main loop
      while (have_posts()) {
        // main query is being used
        the_post();
        $args = array(
          // your query args here ...
        );
        $query = new WP_Query($args);
        if ($query->have_posts()) {
          while ($query->have_posts()) {
            // sub qury is being used
            // do some stuff
            $query->the_post();
            $posts = get_field('performers');
            if( $posts ) {
              // hold the current post in a temp var
              // why below
              $temp_var = $post;
              foreach( $posts as $post) {
                setup_postdata($post);
                // do stuff
              }
              // if we do wp_restet_postdata
              // it resets to the main query, not the secondary query
              // wp_reset_postdata();
              // do this instead
              $post = $temp_var;
            }
          }
          // do some more stuff
          // this resets to the main query
          wp_reset_postdata();
        }
      } // end main loop
    ?>