The solution I went for is to store the ACF data in custom database tables because saving all the CPTs metadata in wp_postmeta
doesn’t t scale especially if you need to do reporting on the data.
The problem and the solution are well explained here
Storing the CPTs metadata in custom database tables might be a good solution to greatly improve the performance of database queries. The problem and the solution are explained here
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.
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.
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?
You’d also need to check for existing values in fields that are empty when submitted but used to have a value and then delete the existing value yourself.
A very good point. Thank you
Hi Keith,
I found the acf-post2post plugin which does exactly what I needed.
Thank you for your time and help.
Thank you for the amazing acf-post2post plugin. This plugin should be in ACF core.
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?
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?
It looks like that plugin does post creation but not post editing/removing so it only solves half the problem.
Do you know of a plugin that can add a new related post, edit and delete an existing related post. I’m after something like this
Thank you
@worksuite I haven’t tested your plugin yet but it looks promising.
Just wondering, what’s the difference between your plugin and this one?
Also, is your plugin compatible with WordPress 4.8.1 and ACF PRO 5.6.0?
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.