Home › Forums › Front-end Issues › Get Relationship from another Relationship
I’ve been struggling with this for a while and tried various methods suggested on here and Stack Overflow, but I just can’t figure out how to achieve what I want.
My setup is as follows:
A Custom Post Typed called ‘collections’ which has an ACF Relationship field called ‘add_to_collection’, which can be used to add 5 other Custom Post Types (‘fan_art’, ‘cosplay’, ‘fan_fiction’, ‘music’ and ‘video_content’)
Each of those CPT’s has the same Relationship field with the same name ( ‘add_to_collection’ ) and they are-bidirectional using the ACF Extended plugin.
So the idea is for users to be able to create a ‘Collection’ of Fan Art, Cosplay, Fan Fiction, Music and Videos. On each of the content single pages, the item can be added to a Collection individually using a front-end form in a modal. And on each Collection page, multiple content can be added to the Collection. I can already output the posts within the Collection fine and filter them by post type on the front end.
I also have counts implemented, showing how many of each post type is in the Collection.
The last thing I’d like to have on each Collection is some information on the *creators* of each content type. To elaborate, each content type (Fan Art, Cosplay, Fan Fiction, Music and Videos) has another Relationship field to select who *created* the content (not necessarily the author of the post as it’s a fan community that archives fan-created content) – Fan Artist, Cosplayer, Writer, Musician and Video Content Creator, respectively. NOTE: Fan Artists, Cosplayers, Writers, Musicians and Video Content Creators are also Custom Post Types themselves. So when choosing who created the fan content, users are selecting from another Relationship field.
I would like to show for example, a list of Fan Artists, Cosplayers, Writers, Musician or Video Content Creators that are featured in the Collection. I assumed you can get the Fan Art, Cosplay, Fan Fiction, Music and Video post ID’s from the Relationship field, but then struggled to figure out how to get fields from another Relationship connected to posts in the first Releationship.
I did figure out how to get the fields following this post, which seemed to be trying to achieve something similar to my issue:
I ended up with the following code:
<h4>Fan Artists Featured In This Collection</h4>
<?php
$items = get_field('add_to_collection');
if($items):
?>
<?php foreach ($items as $i) : ?>
<?php
$artists = get_field('fan_artist', $i->ID);
$size = 'lisf_thumbnail';
?>
<div class="artist-block">
<div class="artist-image">
<?php foreach ($artists as $a) : ?>
<a href="<?php echo get_permalink($a->ID); ?>"><?php echo get_the_post_thumbnail($a->ID, $size); ?></a>
</div>
<div class="artist-title">
<a href="<?php echo get_permalink($a->ID); ?>"><?php echo get_the_title($a->ID); ?></a>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
This gave me the attached image.
However, this isn’t quite what I want. I only want to list each Fan Artist once. I understand that this is within a foreach so that’s why it’s showing the Fan Artist FOR EACH Fan Art item in the Collection, but isn’t that the only way to get the items, by looping through them?
It would be useful to get that original data for the count when wanting to count how many times a Fan Artist is featured in a Collection, but in terms of just getting each Fan Artist once to show, what is the best way? Wouldn’t it need to be a WP Query with the Collection Relationship as the key or something? But I don’t know how to format such a query. Would someone please be able to help me with the full code for what I need?
What I’d also like is to count the creators, so it shows, for example, how many items that Fan Artist is in that Collection. e.g. ‘MaiQueti has 2 Fan Art in this Collection’.
I’ve attached 2 images: one showing how a Collections single template looks now, with a mockup of how I imagine the Featured Fan Artists section looking – with the creator (Fan Artist) data included in the section at the bottom, plus an image showing what the code above is giving me.
To summarise, I want to output the Fan Artists (Relationship/Post Type) connected to the Fan Art (Relationship/Post Type) that is featured in a Collection (Relationship/Post Type). There’s no direct connection of a Fan Artist to a Collection but my understanding is if you get the Fan Art ID, it’s an array that would include the Fan Artist custom field.
And lastly, if possible, I’d want to display the number of a times a Fan Artist is featured in a Collection.
It may be irrelevant if I don’t end up needing the foreach method, but looking at the attached image with my code result, any ideas why I’m getting that ‘Warning: Invalid argument supplied for foreach()’ error too?
Thanks in advance to anyone who is able to help with this.
What you need to do is collect a list of posts, making sure you don’t have duplicates in the first loop and then show them in a different loop.
so, in your nested loop
if($items) {
$collected_artists= array();
foreach ($items as $item) {
// see if we already have this one
if (!is_array($item->ID, $collected_artists)) {
// don't have it, add to list
$collected_artists[] = $item->ID;
}
} // end foreach $item
} // end if items
// Loop over $collected artists and show data for each
if ($collected_artists) {
foreach ($collectec_artists as $artist_id) {
// etc...
}
}
Hi John. Thanks for the response. I’m still struggling to get this working but I think it’s more to do with my inexperience in how to correctly format the whole code. I wasn’t using brackets for the if and foreach statements in my code, but you have in yours and you did use some different variable names to mine, and I’m also unsure what $artist_id is referring to in your code. Is that replacing $artist in my code?
I’d really appreciate it if you could post the whole code as you think it should be based on combining my code with your suggested code.
Which return value should I be using for the relationships too? Post Object or Post ID?
Thanks.
This is the code I ended up with based on what I assumed the structure should be:
<?php
$items = get_field('add_to_collection');
if($items) {
foreach ($items as $i) {
$artists = get_field('fan_artist', $i->ID);
$size = 'lisf_thumbnail';
if($items) {
$collected_artists= array();
foreach ($items as $item) {
if (!is_array($item->ID, $collected_artists)) {
$collected_artists[] = $item->ID;
}
}
}
}
}
if ($collected_artists) {
foreach ($collected_artists as $artist_id) {
echo get_permalink($artist_id->ID); echo get_the_post_thumbnail($artist_id->ID, $size);
echo get_permalink($artist_id->ID); echo get_the_title($artist_id->ID);
}
}
?>
I’m sure it’s wrong but I can’t necessarily figure out why. It’s outputting the data for the Collection itself instead of the Fan Artist for one, no matter which return format I use.
Also, I refer directly to the Fan Artist relationship in the $artists variable but don’t repeat it further down, as in your code you have $items/$artist_id
When using the brackets system for if statements etc, it also won’t let me use html within the code. How can I use html with this formatting?
Lastly, I’m getting this PHP warning too:
“Warning: is_array() expects exactly 1 parameter, 2 given in… etc”
I’m sure it’s getting close but, as I say, I just need help with the formatting/structure because I can’t figure it out so I’d really appreciate it if you could post full code for how you think it should be.
Thanks a lot.
Sorry for the late response. The reason your code is not working is that there was a typo in mine :P. replace is_array
with in_array
Hi John, Thanks for the note about the typo. However, it still doesn’t really help me because I don’t think I’ve formatted the entire code correctly. Your code makes no reference to the fan_artist relationship field, which is why it’s only going to output the Collection itself. So, looking at my code, where am I meant to put your code exactly, while also referencing the fan_artist relationship – which is connected to the fan_art relationship that is in the add_to_collection relationship? I really need to whole code to learn from this.
I appreciate it might be a fair bit of code but I’m sure all the ingredients are there, it’s the formatting I’m struggling with.
Thanks.
The topic ‘Get Relationship from another Relationship’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.