Home Forums General Issues Bidirectional Relationships not updating both sides


Bidirectional Relationships not updating both sides

  • Ok so I have created a support ticket but I haven’t really got any solutions as of yet so I’m trying my luck in here too.

    A bit of a fiddly problem, The site has a set of bidirectional relationships set up via the documentation found at

    The site also uses ACF_LITE. using define( ‘ACF_LITE’, true ); and defining our fields / field groups.

    So an example of a relationship is between the “Vacancies” post type & the “Research Groups” post type.

    Now the problem I have is that if i update the relationship field within the admin area it doesn’t update both post types correctly.

    If i go into a vacancy admin area, add a new research group and hit update. the data is saved.

    I can then navigate to the corresponding research group and see the data is saved within the bidirectional field correctly. BUT the front end output breaks. If i do a var_export on the field name it returns a blank array.

    This can be fixed if I hit save in the research group admin area (even if i don’t change anything). It will then show my updated front end output with all the matching vacancies.

    Whilst not the biggest problem, it is annoying my front end output is breaking everytime I try add a new value into the relationship field. It also negates the point of it being bidirectional relationship if i need to go to the new post and also hit update anyway.

    Any ideas?

    I had a thought that maybe birectional relationships don’t work correctly when using ACF_LITE?

    I did find this link which seems to have a similar problem

    This talks about looking up the values via the database but since we’re using acf_lite i assume our values aren’t saved in the DB and could be the cause? However I’m not sure what would be a good solution for this. Or why it would still display the correct data in the admin area but break the frontend output

  • Take a look at the data in the database. Look at the value of a relationship saved by ACF and look at the value the relationship field saved by your code. Make sure they look the same. What you will see is a serialized array value. Make sure the value type for the post IDs is the same. “s” means the value is a string and the post IDs will have enclosed in “”, while “i” means the value is an integer and the post IDs will not be enclosed in “”.

  • Ok so doing a search for the field name within my database i get the following values:

    Before my update:
    post_id 16095 (Vacancy): a:1:{i:0;s:5:”12729″;}
    post_id 15540 (Research Group): a:1:{i:0;s:5:”15551″;}

    After updating the Vacancy relationship field:
    post_id 16095 (Vacancy): a:2:{i:0;s:5:”12729″;i:1;s:5:”15540″;}
    post_id 15540 (Research Group): a:2:{i:0;s:5:”15551″;i:1;s:5:”16095″;}

    At this point my field var_export() on the frontend of my research group shows a blank array.

    After resaving the research group side of relationship (in order to fix the frontend output, I changed nothing on the page, just hit update):
    post_id 15540 (Research Group) I have: a:2:{i:0;s:5:”15551″;i:1;s:5:”16095″;}

    So from this I can see that my ids have updated to include each other within the database correctly after my initial update and after updating again this changed nothing in the DB.

    So this brings me towards the conclusion of possibly something front end rather than backend causing the issue?

    Why would my var_export() return as blank when the values are saved correctly within the DB after the first update. And why would this issue be fixed when i resave the opposite side of the relationship without changing anything

  • The data looks correct, the question is why? The only thing that I can think of is that the field key reference is wrong.

    After your code updates the field, what is the field key reference? Look at the meta key for the same post, you are looking for the one that has the same name but starts with an underscore. The value of this field will be something like “field_7777777”. Does this field key match the field key of the field you’re updating? Does this change when you re-updated the field by updating the post in the admin?

  • Ok dokey, so undertaking the same process.

    Before my update:
    post_id 16095 (Vacancy): field_58c2e0bbb4904
    post_id 15540 (Research Group): field_58b99b4e8da6c

    After my update:
    post_id 16095 (Vacancy): field_58c2e0bbb4904
    post_id 15540 (Research Group): field_58c2e0bbb4904

    After re-updating my admin post:
    post_id 16095 (Vacancy): field_58c2e0bbb4904
    post_id 15540 (Research Group): field_58b99b4e8da6c

    Ok so looking at this. I can see that the field key references are initially different.

    After updating, the field key references change to the same value (my broken post is the value which changes to match the other)

    And then after re-updating my broken post the field key reference for this post updates back to it’s original value.

    Now this would make me think it was an issue with how the fields are registered initially, but surely they must have different keys in order to show the matching values. e.g. show groups on the vacancies admin or vice versa; show vacancies within the groups admin.

    So perhaps it’s an issue with my update_value call? although I followed the documentation for this to reference to the field name.

    e.g. add_filter(‘acf/update_value/name=research_group_vacancies_relationship’, ‘bidirectional_acf_update_value’, 10, 3);

  • I’d have to see all the code of your filter.

    But, if you have two different fields with 2 different field keys then this is the issue. The code example you’re following is designed to work with fields that have the same field key. In other words, the same field/field_group assigned to both post types.

    Sorry if I missed anything where you said it was two different fields.

    I have a different example that does this for two different fields with different keys here

  • This looks like it should resolve my issue.

    Is there any way of passing the two field keys into the function or will I need to create an entire function for each bidirectional relationship I have since the keys are only set within the function itself?

    Thank you for all your help!

  • There is another example in my repo that was done by someone else. I’m going to be honest, I’ve never tired it.

  • Works perfectly!

    Thanks for your help John

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.