Home › Forums › Front-end Issues › 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:
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!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldnât be so widely used in WordPress if it didnât have some pretty amazing capabilities. In this article, we look at a few of the features weâll discuss during â7 things you didnât know you could do with ACFâ at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.