Support

Account

Home Forums Front-end Issues Multiple Relationship Field Calls on a Single Query

Solving

Multiple Relationship Field Calls on a Single Query

  • Hi there!

    So in short, I’m trying to place multiple relationship calls on a single query, and I seem to almost have it… well.. sort of.

    I have 3 custom post types:

    1. Featured Games (featured_games)
    2. NCAA Teams
    3. NFL Teams

    The NFL and NCAA teams all are simple posts with a title and a featured image. The featured image is the team’s helmet.

    The Featured Games section has a field group that includes 2 relationship fields and a single select field.

    The 2 relationship fields are limited to only 1 selection each and appear as a select of the combination of both NCAA and NFL teams.

    The Select field is simply As or Vs.

    My end goal is to create a featured game, select the home and visiting teams, and select at or vs. Then feed that data into a widget-like area.

    The featured games, ncaa, and nfl custom post types all seem to be working correctly. The custom field types also seem to be working as expected in the backend. Now I’m just trying to display the data to the front end.

    My current code is…

    <b> Featured Games </b>
    
    <?php 
    
    $the_query = new WP_Query(array(
    	'post_type'      	=> 'featured_games',
    	'posts_per_page'	=> 15,
    	'post_status'		=> 'publish',
    	'orderby'        	=> 'date',
    ));
    
    if ( $the_query->have_posts() ) :
    	while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
    	<div class="row">
    		<div class="col-md-5">
    			
    		<?php	$home = get_field('home_team');
    		if( $home ):
    		
    					foreach( $home as $post):
    						setup_postdata($post);
    						the_post_thumbnail();   
    					endforeach;
    					wp_reset_postdata(); ?>
    			
    		<?php endif; ?>
    		</div>
    		<div class="col-md-2">
    			<?php 
    				
    				
    				the_field('vs_or_at'); 
    				
    				
    				
    				?> 
    		</div>
    		<div class="col-md-5">
    		<?php $visiting = get_field('visiting_team');
    		if( $visiting ): ?>
    			
    				<?php 	foreach( $visiting as $post): 
    						setup_postdata($post);     
    						the_post_thumbnail();
    					endforeach; 
    					wp_reset_postdata(); ?>
    			
    		
    		<?php endif; ?>
    		</div>
    	</div>
    	<?php  endwhile;
    else : ?>
    	<?php _e( 'Sorry, no Featured Games Available' ); ?>
    
    <?php endif; ?>

    This code successfully loops the first team helmet and then fails.

    If I remove everything in between the first div column, then it successfully shows the vs/at and the visiting team helmet.

    So I believe my code is pretty close to correct, I’m just missing something obvious that’s causing it to fail after completing the first get_field query.

    Any help would be super appreciated! Thank you! 😀

  • Okay, I made some progress! 🙂

    I realized that the reason the code loop didnt keep running is because I needed to keep calling in a new query for the other pieces. My new code is:

    <b> Featured Games </b>
    
    <?php 
    $args = array (
    	'post_type'      	=> 'featured_games',
    	'posts_per_page'	=> 15,
    	'post_status'		=> 'publish',
    	'orderby'        	=> 'date',
    );
    $the_query = new WP_Query($args);
    if ( $the_query->have_posts() ) : ?> 	
    	<div class="row">
    		 
    			<div class="col-md-5">
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
    						<?php	$home = get_field('home_team');
    						if( $home ): ?>
    						<?php		foreach( $home as $post):
    										setup_postdata($post);
    										the_post_thumbnail();   
    									endforeach;
    									wp_reset_postdata(); ?>
    							
    						<?php endif; ?>	
    				<?php  endwhile; ?>
    			</div>
    
    			<div class="col-md-2">
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 				
    						<?php the_field('vs_or_at'); ?> 		
    				<?php  endwhile; ?>
    			</div>
    	 	 
    			<div class="col-md-5">
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
    						<?php $visiting = get_field('visiting_team');
    						if( $visiting ): ?>
    								<?php 	foreach( $visiting as $post): 
    										setup_postdata($post);     
    										the_post_thumbnail();
    										endforeach; 
    									wp_reset_postdata(); ?>	
    						<?php endif; ?>
    				<?php  endwhile; ?>
    			</div>
    		  
    	</div>
    <?php endif; ?>	

    Now my problem is instead of looping through and showing one of each, then repeating, I’m getting all of one, then all of the next, then all of the third.

    So ideally I’d like to see:
    Home Team vs/at Visiting Team
    Home Team vs/at Visiting Team
    Etc..

    But right now I’m seeing
    Home Team Home Team … vs/at vs/at … Visiting Team Visiting Team

    I’m sure it’s a loop thing… but I can’t quite put my finger on it… I know it’s going to be real obvious once I (or someone else) finds it. haha

    Thanks again in advance!

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

The topic ‘Multiple Relationship Field Calls on a Single Query’ is closed to new replies.