I’m trying to create a bidirectional relationship: FatherOf, wwhere I could display on the father’s page: “Children(s): Lucy”, and on the children’s page: “Father: John”.
I’ve tried pretty successfully the Advanced Custom Fields: 2-Way Post Relation, but then I also tried to use it combined to Quick and easy Post creation for ACF Relationship Fields, I realized I couldn’t combined them (the first plugin add a new type of relationship the second one can’t reach). When I select a child on the father’s page, I can’t add a non existing one from there. It seems I had to choose between the two extensions. 🙁
Then I found https://www.advancedcustomfields.com/resources/bidirectional-relationships/ Unfortunately, I encounter two issues. The script is (only) good for sibling relationships, and I can’t create more than one “related_posts” relationship.
May someone help me to adapt the script? Hope this request is clear enough and in the right forum.
I have a plugin that works with standard relationship and post object fields, but I do not know if it will work with the Quick and easy Post creation for ACF Relationship Fields plugin. https://github.com/Hube2/acf-post2post. It’s pretty basic and requires that the relationship field is named the same on all of the post types. There are no settings.
Sorry, I should have asked that first. I can’t make my plugin work in ACF 4, it only works in ACF 5. The routine that finds the same field for the post that you’re adding to the relationship depends on a function in the Pro version that does not exist it the version you’re using.
I would be possible to build something similar for ACF 4 for a specific field.
This is what it does. When the relationship value is save using the acf/update_value filter https://www.advancedcustomfields.com/resources/acfupdate_value/, the new value of the field is compared to the old value of the field. If there is a difference then updates are made to the related posts. If there was a relationship in the old value that is no longer there then the other post is updated to remove the reciprocal relationship. If there is a relationship in the new value that was not in the old value then the other post is updated to add the reciprocal relationship.
50% of the code in the plugin is just to automatically find the same field on the other post.
All right John, no problem. 😉 Thank you for your detailed explanations.
I may buy ACF Pro, but before, I’d like to be sure, with your plugin, I would have dedicated labels for different CPTs, or taxonomies. I’ma try to be more precise by giving an example. Imagine an artist page, and an album page. Would I be able to have “Album:” label on the artist edit page, and “Artist:” label on the album edit page? Of course, I don’t want to create the relation twice.
If I can find some time I hope to create and example of how to do this for a single field. 2 way relationships is something that is requested a lot and it’s actually easy enough to do for a single field.
As far as your question, yes. You could define different fields with different labels. The only thing that matters as far as that plugin is concerned is that the field names are the same and that they are either a relationship or a post object field.
This will be easier if the fields that you’re using are better defined. You have Father (or parent) and Child. A Father can have multiple children but a Child can only have on Father? Well, I guess there are several ways to have multiple fathers today….. anyway. What are the field types and settings for the fields on each of these that you’d like to have? Trying to account for every possibility makes the code more complex.
I created a simple example of a relationship field here https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-reciprocal-relationship.php
Thank you for the script John!
In fact, I have a thesaurus build with (CM Tooltip glossary premium), where we can find simple concepts, best practices, and actions. They’re all of the same type, only their taxonomy is different. Say I want to link a best practice to a concept. The name of the relation is “acf_reciprocal_relationship”. The field is used in two group fields, and the title of the field is “Test”.
1. I put the script in the functions.php theme’s file and I uploaded it.
2. I went to ACF 4, and created a new standard relationship named “acf_reciprocal_relationship”.
3. In a concept edit page I selected two best practice and I published the post.
4. I refreshed then the associated best practice edit page: nothing shows up. I can’t see any relationship (the field is empty). 🙁
It will only work with posts, not taxonomies. Did you edit the field keys to match the relationship fields that you’re trying to connect? The code that I created will only work on one set of relationships. You’d need to create a new filter for every set of relationships that you want to use it for.
Another thing you might be running into is that you’re trying to use it on a relationship field that already has relationships, this will not work. The filter compares the old values with the new values so that it knows when relationships need to be removed and which relationships to remove. If you add this filter and then just update a post, nothing will happen because nothing has changed…..
I made a change to the script that will make it work on existing relationships fields with existing values, please read the comment in the file for more information
When I was talking about taxonomy, I did not expect to add a field to a taxonomy, but to filter same post type with taxonomy. 😉
I uploaded the functions.php, with the new script: a syntax error appears on line 108. In fact, this error appears when I want to go in the WP backend. The screencast.
Did you edit the field keys to match the relationship fields that you’re trying to connect?
I should edit your function script before I upload it, right?
I fixed the syntax error.
Yes, you should edit it to change the field keys to match the field keys of the relationship fields you want to make 2 directional. If it’s the same field then put the same field key in both places, but remove the second
The file I put on github is meant to be an example and there’s a lot of comments in it to explain what’s going on. I use the repo to post many examples of code for doing things in ACF.
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!
© 2023 Advanced Custom Fields.