Support » Plugin: Advanced Custom Fields: Extended » How to link existing relationships

  • Resolved anjanphukan

    (@anjanphukan)


    Hi,

    I have 2 post types – Product and News. Before installing this plugin, I had ACF’s default Relationship field in News post types to link them to Products.

    Now I found this very useful plugin to set Bidirectional Relationship between posts. I have created a new Relationship field for Products now and set it as Bidirectional and linked with the field in News.

    Now the problem is the field in Products doesn’t get updated automatically from the values from the field in News. To link them first I have to go to a News and then delete the relationship first and then save the news and then re-add the relationship in the news again. This time the field in Product get updated and links with the respective News. This happens might be because of the reason that the News relationships are added before the installing of this plugin.

    I have almost 200 News now and all of them are linked to the related products. To go to each News and first remove the relationship and then add again will take lots of time. So I was wondering if there is any easy way to update all the Products and News relationships.

    Please let me know if you have any questions.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author hwk-fr

    (@hwk-fr)

    Hello Anjanphukan,

    In fact, the bidirectional setting checks the old value and compare it to the new values before updating the related fields. This behavior is applied to avoid unecessary database calls.

    In your situation, there should be a way to force the system to update related fields, even if values have not changed. I’ll add a specific PHP filter allowing this, so it will help users like you who are upgrading with already existing values.

    I’ll prepare a solution this weekend and answer you as soon as the patch is up 🙂

    Thanks for your patience!

    Regards.

    anjanphukan

    (@anjanphukan)

    Hi @hwk-fr

    That’s wonderful. Thanks for your help.

    I will wait for the update. Please let me know if I need to make any further adjustment in to it.

    Regards,
    Anjan

    Plugin Author hwk-fr

    (@hwk-fr)

    Hello Anjanphukan,

    Just to let you know that the latest ACF Extended 0.8.2 added your feature request. In order to force field update, you can add the following code in your functions.php:

    
    add_filter('acfe/bidirectional/force_update/name=my_field', 'my_acfe_force_update', 10, 3);
    function my_acfe_force_update($force_update, $field, $post_id){
        
        return true;
        
    }
    

    Once added, everytime you’ll save a post with the bidirectional meta field my_field, it will update all related fields. You can do this manually in the administration, or you can run a script and update the field using update_field('my_field', array(1,2), $post_id), it’s compatible 🙂

    Once you’re done, you can remove the code from your functions.php file.

    Have a nice day!

    Regards.

    Thank you so much for the quick turnaround. I updated the plugin and put the code in my child theme’s functions.php file and the manual process worked.

    But to be honest, I am slightly confused about the automated way using update_field('my_field', array(1,2), $post_id). Wondering if I need to put it inside a hook or something. My field name in Product is product_news_rel and in News is news_product_rel. So in News products are already added using ACF default relationship field. So I am sure my_field will be replaced by news_product_rel but I am confused about the array(1,2). Do I need to replace it with something? Any suggestion?

    Thanks again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.