Support

Account

Home Forums Add-ons Repeater Field Programmatically update row index number for rows of a repeater field

Solving

Programmatically update row index number for rows of a repeater field

  • I’ve been searching for hours for a solution to what I feel is a common issue with rows of a repeater field.

    Basically I have a repeater field that holds links users can create. The rows have several fields (url, image, display name, etc). I have created a form on the front-end to add/update/delete these links which all works fine (not using the acf_form).

    What I am looking for is a way for the users to update the row order (index) on the front-end like you can do on the back end by dragging and dropping. I don’t need the headache of trying to recreate the drag & drop functionality I just need a way to update the row index. I understand I can get the row index using get_row_index but how do you update that number? This is essential because these links are used to create user pages and I have to give users a method to change the order of how they appear on their page without the use of the drag and drop functionality of acf_form.

    Am I overthinking thinking this? Should I instead just add another field (ie. display order) to the row and use that for my sort? It seems like there has to be a way to do this using built-in functions. I can’t be the only person that has to custom sort repeater rows right? 🙂

  • The only way to change to order of the rows from a coding standpoint is to get the entire repeater as an array, reorganize the array and then re-save the entire repeater. the index of a row is only the order of that row in the array.

    
    $repeater = array(
      array(
        // row array
        'field_1' => 'value',
        'field_2' => 'value'
      ),
      array(
        // row array
        'field_1' => 'value',
        'field_2' => 'value'
      )
    )
    
  • Hey John, thanks for the reply!

    What I’ve done instead is added a unique ID to each row using code from a previous post I found. Now I only need one more thing…

    The unique ID is only saved to the row after the first “save” of the row data. Since I am adding/updating/editing the rows using update_sub_field function the “save” never fires. Is there a way to recreate pushing the update button of the acf_form. I don’t need it to update any data, I just need it to fire the creation of the unique ID.

    Ps. Are you the only active person on this forum? It seems like I see your name everywhere! 🙂

  • As far as I know, there isn’t a way to fire the save. ACF is fired to save when a post is saved or a term and there is something custom in options pages, but I think it still happens on a WP hook.

    What you’ll need to do is check do a check of the existing rows when a row is updated and create the ID for rows where it is missing.

    PS: There are other people that answer questions. I’m as active as I am because, well, I am paid help, or give what help I can. I can’t answer every question because some of them are beyond my knowledge. This is a user forum and depends on users. In the end I am really just another user, I have been using ACF for so long that I can’t remember building a site without it and I also like helping others if I can. Other people like me have come and gone, it’s not an easy job and can be frustrating at times.

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

You must be logged in to reply to this topic.