Home › Forums › General Issues › Querying Relationship Fields, Two-Way Linked Meta Box
I’ve found a doc online which partly address what I was hoping to achieve with ACF, which is to create a many-to-many relationship post types (in example below doctors & locations), for which the relationships are updated simultaneously on both of the custom post’s meta boxes.
The tutorial link below seems to achieve the relationship, but also noticed that on Locations custom post, it does not have a relationships custom field that shows the number of doctors that work at the location.
In my case, I’m using Movie & Actor custom post types.
I tried adding the relationship custom field to both Movie and Actor CPT, and when I assign the actor cast on Movie page, and then go to the actor’s page edit, the relationship custom field doesn’t show the movie that was previously linked on the actor page.
Also referred to this link above which shows a snippet which per description looks to address the issue I was having but this may be related to a single post type (maybe not for relationship between 2 CPTs?), anyhow this snippet on functions.php did not seem to resolve it.
Thank you so much!
ACF does not automatically update relationships on the other end, that is what https://www.advancedcustomfields.com/resources/bidirectional-relationships/ addresses. Alternately you can use this plugin https://wordpress.org/plugins/post-2-post-for-acf/.
By using the above code or the plugin you remove the need to do a reverse relationship query as described in the first document https://www.advancedcustomfields.com/resources/querying-relationship-fields/
thank you John.
Yea, as I’ve attempted before, that bi directional snippet didn’t work for me, and I’m guessing its because that doesn’t solve the relationship between two CPTs?
The plugin also says it only for ACF5 which I have not opted in for.
Is there a way to modify the bi-directional snippet to support two CPTs relationship?
Ignore the above, I’ve just upgraded to early access 5 and installed the plugin to get it working.
Thank you so much.
I’ll still post this for anyone else that wants to get this running on ACF4. Three are two examples of doing this for ACF4 in this repo https://github.com/Hube2/acf-filters-and-functions
1) Can the snippets on repo be used on ACF5 as well? I ask this since i’ve already upgraded, and it appears the snippets allows for linking of two different field names, as opposed to the plugin which requires the field names to be identical.
2) On this “acf-reciprocal-relationships-multiple.php”, is the Reciprocal relationship example 1 and Reciprocal relationship example 2 identical?
The first one is what I coded, it only handles 1 reciprocal relationship.
The second one was contributed by someone else. It is supposed to work with multiple fields. To be completely honest, I have not tested that one out.
I know that the first one will work with either ACF 4 or 5.
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!
🗓️ Just one more day until the next session of ACF Chat Fridays! Make sure to register for the latest updates on what’s coming in ACF 6.1!— Advanced Custom Fields (@wp_acf) March 30, 2023
📆 Friday 31st March 3pm UTC
👉 Register here - https://t.co/3UtvQbDwNm pic.twitter.com/7xtEJakeQN
© 2023 Advanced Custom Fields.