Home › Forums › Feature Requests › View Related Records
Hi,
Let’s say I have two CPTs “Books” and Authors”. When I add a new Book I select the author(s). Then when I edit an Author I’d like to see the related Books for that Author.
Pods 2.7-beta have achieved this by using Bidirectional Relationships between CPTs. Watch the video at https://pods.io/2017/05/30/pods-2-7-beta/
Does ACF plan to add this feature or is there a way I can display the Related Books for an Author on the back-end?
Hi Keith,
That’s a great example for the front-end. How do I show the doctors that work in a location on the back-end (i.e when I’m on the “Edit Location” screen)? What’s the action/filter that I need to use to display the list of doctors under the Address field?
Hello…
Good question…. here’s how I might tackle it…
First… as we know…
We have a ‘Doctors’ CPT
… with a ‘Locations’ Relationship field.
This ‘Locations’ field may have a series of Location IDs (say: 5, 6, 7)
Now, we have the ‘Locations’ CPT
When looking at ‘Edit Locations’
… we could have a ‘Doctors’ Relationship field here.
In it… we could work this hook…
… to fetch all the Docs that are attached to that Location.
Now, as an alternative… if you just wanted to do this for ‘Display’ purposes, you can pre-populate another field with the doc data… just use some other field type other than Relationship… like the ‘Enhanced Message’ add-on that allows for PHP, or maybe some other built-in field type like textarea, and make it read-only. Example here for making it read-only.
But… it’s better probably to use a Relationship field to make for smarter editing. But we have to keep the data in synch on our own from the way I understand it. So… each time a change is made to the ‘Doctors’ Relationship field when editing ‘Locations’, you can write logic that loops through and updates the ‘Doctors’ records (i.e. adds a location or removes a location).
Now, I haven’t thought far enough ahead to point you how to do the last part, because I’m not sure you wanted to get this far down the rabbit hole 🙂 Let me know!
Hi Keith,
But… it’s better probably to use a Relationship field to make for smarter editing. But we have to keep the data in synch on our own from the way I understand it. So… each time a change is made to the ‘Doctors’ Relationship field when editing ‘Locations’, you can write logic that loops through and updates the ‘Doctors’ records (i.e. adds a location or removes a location).
This is exactly the solution I’m after. I’d like to setup bidirectional relationships between ‘Doctors’ and ‘Locations’ so when I edit a ‘Location’ I can also add/remove ‘Doctors’ from that ‘Location’ and vice versa.
This would be a great Guide on how to create Bidirectional Relationships between two different CPTs.
Any idea on how I can do this?
Hello,
Each time a post is saved, you can filter the values:
https://www.advancedcustomfields.com/resources/acf-save_post/
Hi Keith,
I found the acf-post2post plugin which does exactly what I needed.
Thank you for your time and help.
Here is a link in case anyone wants a native solution to Bidirectional Relationships using just ACF.
Read the Example below the code first, then, sifting through the code may make more sense. It introduces code that may be a little foreign, but the solution works really well, and it is flexible too!
Hi Keith,
I tried the ‘native’ solution and I couldn’t make it work for creating bi-directional relationships between CPTs.
In my project I have eight CPTs which need to be linked to each other by one-to-many and many-to-many relationships by making Relationship and Post Object fields biderectional.
The acf-post2post plugin worked perfectly out of the box.
Does the native solution work for creating two way (bidirectional) relationships between DIFFERENT post types? If so, how?
yes, different CPT’s… one-to-one, one-to-many, many-to-many 🙂
Hi Keith,
I tried it again and it doesn’t work for a post_object field that does not allow multiple values.
If you look at the code you can see that it’s looking for an array value:
// loop over selected posts and add this $post_id
if( is_array($value) ) {
foreach( $value as $post_id2 ) {
I would certainly like to use the ‘native’ solution if there is a way to get it to work with single values.
Hello… rather than re-do the code… what about a workaround to ‘cast’ your singular value as an array?
Sample:
<?php
$value = 1;
$value = (array)$value;
if ( is_array($value) ) {
echo 'Yes, it is an array!';
} else {
echo 'Nope, not an array :(';
}
?>
Hi Keith,
Even casting the singular value as an array it still doesn’t work for one-to-many relationships. It works fine for many-to-many relationships.
I have two CPTs (Companies and Addresses) where one company can have many addresses and each address can only belong to one company.
So when I add/edit an Address I select a Company (which is a post_object field with multiple values DISABLED called ‘rel_com_addr’), however, when I add/edit a Company I can select multiple Addresses (which is a post_object field with multiple values ENABLED called the same ‘rel_com_addr’). In this scenario the native solution doesn’t work.
So the ‘native’ solution only works if you are using a Relationship field or a Post Object field with multiple selections enabled. Even the tutorial states that:
This tutorial uses a Relationship field to select posts, however a Post Object field with multiple selections enabled would work just the same as both field types save data as an array of post ID’s.
I hope that the code in the tutorial will be updated so that it also works with one-to-many relationships.
The topic ‘View Related Records’ 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.