Support

Account

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

Helping

Relationship query from custom post type on external page

  • On my homepage I want to feature the information of the next coming event (custom post type), this is working and it displays all the info from the event post – except the info from the Relationship field.

    The below code works on the single-event.php page but within the loop on the homepage it can’t grab them – can anybody explain why?

    
                <?php
                            $today = current_time('Ymd');
    
                            $args = array(
                                                'post_type' => 'event',
                                                'post_status' => 'publish',
                                                'posts_per_page' => '1',
                                                'meta_query' => array(
                                                array(
                                                    'key' => 'date',
                                                    'compare' => '>=',
                                                    'value' => $today,
                                                )
                                                ),
                                                'meta_key' => 'date',
                                                'orderby' => 'meta_value',
                                                'order' => 'ASC',
                                                'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
                                            );
                                            $query = new WP_Query($args);
                                            if ($query->have_posts()) :
                                            while ($query->have_posts()) : $query->the_post(); 
                                        ?>
                                            <tr>
                                                <td class="gig_date2">
                                                    <?php $date = get_field('date');
                                                        $dateD = date("d", strtotime($date));
                                                        $dateM = date("M", strtotime($date));
                                                        $dateY = date("Y", strtotime($date)); 
                                                    ?>
    
                                <?php echo $dateM; ?> <?php echo $dateD; ?> <?php echo $dateY; ?> @ <?php the_field('venue'); ?>
                                     
                        <div class="entry-content">
                            <strong>Date:</strong> <?php the_field('date'); ?>
    
                            <strong>Venue:</strong> <?php the_field('venue'); ?>
    
                            <?php 
    
                            $posts = get_field('performers');
    
                            if( $posts ): ?>
                                <ul>
                                <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
                                    <?php setup_postdata($post); ?>
                                    <li>
                                        <?php the_field('name'); ?>
                                    </li>
                                <?php endforeach; ?>
                                </ul>
                                <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
                            <?php endif; ?>
                            
    
                        </div>
    
            <?php endwhile; ?>
            <?php endif; ?><?php wp_reset_postdata(); ?>+ LINE-UP +</h2>
    
    
  • 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
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Relationship query from custom post type on external page’ is closed to new replies.