Bidirectional Relationships in ACF

This was asked many times and the default answer “impossible” is right most of the times. But I have a special constellation that works with a little tweak:

What I wanted to do

I have products and suppliers. One product can have many suppliers, one supplier can offer many products.

Adding a supplier to a product should be accompanied by a note, e. g. “This is the cheapest supplier for this product, but they ship only on Fridays”.

Means two steps:

  • A bidirectional relation
  • A repeater-field for the relation and the note

It is near impossible to edit the relation from both sides. Adding a new related product to a supplier would have to create a new row in the repeater field, select the product as relation and … we do not care. Fortunately we only want to add related suppliers (including notes) and not vice versa.

So the final solution would be:

  • Add or edit a relation to suppliers in products, optionally add a note for this relation
  • Output in products: Company | Note
  • Output in supplier: Product.
  • Yep, no possibility to edit the relation from the supplier side, we want that

What I did

I tried a couple of things:

What finally worked

Creating a bidirectional relationship with ACF Pro is pretty easy using one of the four solutions above. You only need to have a field on the toplevel (means not inside a repeater or similar). That means the easiest and most stable way is

  1. Create your two relationship fields and make them bidirectional using one of the four solutions
  2. Create your repeater with an relationship field inside (Post Object works best)
  3. Add the following code to your functions.php, change the three variables to fit your fields:
PHP

This function updates the relationship-field from the single post objects in my repeater. For me the repeater fields are the single source of truth. That means:

  • If you delete all rows in the repeater, the contents of the relationship field will be deleted.
  • If you add or delete row in the repeater, the contents of the relationship-field will be written as whole new value instead of appending new rows to the existing values.
  • You should now hide the two relationship fields from edit to not confuse editors.

Have a look

And what about performance?

With a few hundred suppliers and a few thousand products in my platform I will not run into performance problems. If you have to cope with the same task but with a lot more post objects, you may consider using https://hookturn.io/downloads/acf-custom-database-tables/. I’m glad my solution worked without another premium plugin, but that would have been my next attempt.

Have some other idea for relationships, repeater fields and stuff like this? Leave a comment …

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top