php - Wordpress ACF relationships
Overview
So I've got two separate custom post types. Bands & Videos. When videos get added, you can select if a band appears in the video. I then use:
<?php if( get_field( "featured_band" ) ): ?>
<div >
<h3><a href="<?php the_permalink(); ?>"><?php the_field('video_title') ;?></a></h3>
<?php
$bands = get_field('featured_band');
?>
<?php if( $bands ): ?>
<p>Bands in this video:
<?php foreach( $bands as $band ): ?>
<span ><a href="<?php echo get_permalink( $band->ID ); ?>">
<?php echo get_the_title( $band->ID ); ?>
</a></span>
<?php endforeach; ?>
</p>
<?php endif; ?>
<div ><?php the_field('video_embed_code') ;?></div>
<div ><?php echo custom_field_excerpt(); ?></div>
</div>
<?php else : ?>
<?php endif; ?>
This looks at which band is added and loops them each and spits it out as a link to which someone can click and it takes them to that bands page. Perfect. But now i want to go to the bands custom post type and make any videos with the band featured, appear on the bands-single.php
How would i do this with the advanced custom fields plugin?
I did try:
<?php if( get_field( "video_title" ) ): ?>
<div >
<h3><a href="<?php the_permalink(); ?>"><?php the_field('video_title') ;?></a></h3>
<?php
$bands = get_field('video_title');
?>
<?php if( $bands ): ?>
<p>Bands in this video:
<?php foreach( $bands as $band ): ?>
<span ><a href="<?php echo get_permalink( $band->ID ); ?>">
<?php echo get_the_title( $band->ID ); ?>
</a></span>
<?php endforeach; ?>
</p>
<?php endif; ?>
<div ><?php the_field('video_embed_code') ;?></div>
<div ><?php echo custom_field_excerpt(); ?></div>
</div>
<?php else : ?>
<?php endif; ?>
What i can't get my head around is how the relationship works from the bands points of view. Help would be appreciated. Please let me know if you need to see more of the code.
Answer
Solution:
If the relationship is defined by selecting related bands in the video custom post type then in order to get the videos for a particular band you need to do a reverse lookup. As far as I'm aware there isn't an advanced custom field function to do this for you so you need to write your own query. Something like this:
Answer
Solution:
From the ACF site: http://www.advancedcustomfields.com/resources/relationship/
"It is possible to perform a reverse query on a post (post A) to find all the posts (post B, post C) which have selected it (post A). To learn more about a reverse query, please read this in-depth tutorial: http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/"
Two way querying is covered quite comprehensively in those two pages.