Support

Account

Home Forums Add-ons Repeater Field Create New Repeater Row from Front End Form Reply To: Create New Repeater Row from Front End Form

  • Hi Joe,

    Do I understand you correctly if I think you have a larger field group which contains amongst else a repeater field? And the repeater field in term contains multiple fields including a google map field.

    I think the easiest way for you to do this would be to create another field group for your CPT. In it you’ll put just a single google maps field. Then set the field_groups parameter of acf_form() to the new group so that a visitor only see this single google maps field.

    Then you hook into the save_post action. Make the necessary checks to make sure it’s your CPT and a value has been set to the new google maps field.

    Here’s the tricky part. There is no easy way to add a new row programmatically to ACF. There’s only the update_sub_field function which are capable of updating an existing subfield value.

    So what you need to do is first fetch the repeater fields value directly from the DB (which is a number corresponding to how many rows there are). Update it with +1 and then you can use update_sub_field.

    I’ve done this myself in a project recently and it works 🙂

    Here’s some sample code for how to create a new row and add values to it from within functions.php

    
    //Our single google maps field
    $temp_map = get_field('field_name');
    
    //Repeater field
    $repeater = get_post_meta($post_id, 'field_name', true);
    
    //If there are no rows yet just set to 1, otherwise +1
    $new_repeater_count = ( !$repeater ? 1 : $repeater + 1 );
    update_post_meta($post_id, 'field_name', $new_repeater_count);
    
    //Now we can start adding our sub field values. However the update_sub_field function takes 0 as base so we actually need to subtract 1 from our $new_repeater_count
    $index = $new_repeater_count -1;
    //Update the map sun field
    update_sub_field( array( 'repeaterfieldkey', $index, 'subfieldkey' ), $temp_map), $post_id );
    
    //And now we want to clear the single google maps field since there shouldn't be any preset address for the next person!
    update_field('fieldkey', '', $post_id);
    

    Let me know how that works out!