Support

Account

Home Forums Add-ons Repeater Field Override issue when repeater is populated both via backend and frontend

Solved

Override issue when repeater is populated both via backend and frontend

  • I use a repeater field for creating a visitor list:

    
        First Name    Last Name
    ---------------------------
    1   John          Miller
    ---------------------------
    2   Tom           Brown
    ---------------------------
    3   Mike          Smith
    ---------------------------
    ...
    

    My field can be populated in two ways:

    1. via the WordPress backend (when I edit the post containing the field)
    2. via a form in the frontend of my site (I use the add_row() function when the form is submitted.)

    My problem:

    • I edit the related post in the WordPress backend. There, I see a list of 3 visitors.
    • In the meantime, someone enters a 4th visitor via the frontend form. The 4th visitor is saved to the DB.
    • If I refreshed my page in the backend, I would see that a 4th visitor was added. Fine!
    • But if I don’t refresh my page in the backend (i.e. I still see the list of 3 visitors) and hit the post “Update” button, the list of 3 visitors is saved and overrides the list that actually contains 4 visitors. The 4th visitor is lost.

    So, my question is:
    How can I prevent this? How can I check if new entries were added to the list before it is saved? I guess, I could use the acf/update_value filter or the acf/save_post action – but I don’t know how.

    Thanks a lot,
    Karl

  • I have not done this and I don’t know all the details but.

    I would create an acf/save_post filter with a priority of 1 so that it runs before ACF saves the values. See details here https://www.advancedcustomfields.com/resources/acf-save_post/

    In this filter I would somehow check the existing value against the submitted value and update the field as needed. This is where I don’t have the details. It would take a lot of testing to figure out how to accomplish it on both the front and back end.

    When I was done I would unset any relevant field keys in $_POST[‘acf’]. Removing the submitted values will prevent ACF from updating those fields and undoing the work you did in the filter.

    This is an issue when someone tries to edit both sides of a bidirectional relationship at the same time. I tell my clients not to do it. However, if there is no way to prevent this from happening then I’d have to work it out.

  • I have reconsidered my approach and came to the conclusion that it’s definitely not a good idea to make the list editable from both sides. Your answer was very helpful to me and confirmed my worries.

    Thank you very much, John.
    Karl

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

You must be logged in to reply to this topic.