Support

Account

Home Forums Add-ons Repeater Field Add Row to Nested Repeater Field

Solving

Add Row to Nested Repeater Field

  • Hey Elliot – firstly, thanks so much for this amazing series of plugins. Not a day goes by that I don’t marvel at the ease and power that it adds to our development process!

    I’m attempting to build a plugin that allows users to create lists of posts, sort of like wishlists. So the user will be able to create a new list, name it, and then save post IDs to those lists. The way I am implementing it is through a nested repeater field associated with the user.

    nested repeater

    On the post page, they can select from a dropdown of their existing lists, or they can type the name of a new one into a text box. Adding a new list is easy enough by passing an array into update_field, but how would I allow them to add a post ID into an already existing list?

    Hope I explained that well enough, thanks!

  • Hi @jtnatoli

    Currently, the update_function will override the current value completely, which makes adding a row a bit of a hastle.

    You will have to use the get_field function to load the entire repeater (parent repeater) and then add your new row to the nested repeater.

    So, using the name of the list, you could get the parent repeater, loop through the rows and when you match the list name to that row’s list name value, you can then add to the corresponding causes.

    Then, use the update_value function on the updated value.

    Good luck!

    Cheers
    E

  • Hey thanks Elliot, I’ve figured this out, however, now I am trying to figure out how to remove a nested repeater row. It seems simple enough using code like: do_action('acf/delete_value', 101, 'causes_1_cause_id' );

    Doing this removes the field from the database, however, it does not update the count field in the database and it does not decrease each subsequent row number by one. As you can see, running the code above results in the table looking like the screenshot below:

    db

    causes should have been changed to 2 and (_)causes_2_cause_id should have been changed to (_)causes_1_cause_id

    Is there a built-in function that will allow me to do this, or some direction on how to accomplish it?

    Thanks again!

  • Hi @jtnatoli

    Unfortunately, removing a sub field value like you have tried is not supported in the repeater field.

    You will need to modify the entire value and use the update_value function to save it.

    Sorry that it can’t be done any easier than that

    Cheers
    E

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

The topic ‘Add Row to Nested Repeater Field’ is closed to new replies.