Support

Account

Home Forums Add-ons Repeater Field Add Repeater Row From Another Field

Solved

Add Repeater Row From Another Field

  • Hi there,

    I have a front end form using acf_form() where I’d like front-end users to add a row to a repeater from a textarea. In other words, I don’t want users to be able to edit/change rows, only add them via a textarea field (and then reset said textarea)

    I’ve checked out add_row() and suppose that this needs to be used in conjunction with acf/save_post

    My code:

    
    function add_repeater_row_on_save($post_id) {	
    	
    	$my_textarea = get_field('my_textarea'); // My textarea field
    			
    	$row = array(  // my repeater sub-field
    		'field_1s'	=> 'Repeater sub-field 1 text',
    		'field_2S'	=> 'Repeater sub-field 2 text',
    		'field_3s'	=> 'Repeater sub-field 3 text',
    	);
    	
    	$i = add_row('field_repeater', $row);
    	
    	// Default Meta Key Array
    	$reset_textearea_value = array(
    		'my_textarea' => '',
    	);
    
    	
    	// Set Default Meta Keys Loop
    	foreach($reset_textearea_value as $meta_key => $meta_value) {
    	   update_post_meta($post_id, $meta_key, $meta_value);
    	};
    	
    };
    add_action('acf/save_post', 'add_repeater_row_on_save', 20);
    

    The above code does update my textarea, just not the repeater bit.

    Thanks in advance for any insights!

  • Sigh, sometimes I feel like when I post here, the solution is about 5 minutes away. Turns out that I was missing the $post_id in the add_row() function.

    So, the code that I have and is now working is:

    
    function add_repeater_row_on_save($post_id) {	
    	
    	$my_textarea = get_field('my_textarea'); // My textarea field
    			
    	$row = array(  // my repeater sub-field
    		'field_1s'	=> 'Repeater sub-field 1 text',
    		'field_2S'	=> 'Repeater sub-field 2 text',
    		'field_3s'	=> 'Repeater sub-field 3 text',
    	);
    	
    	$i = add_row('field_repeater', $row,$post_id); // The $post_id here was missing
    	
    	// Reset Textarea
    	update_field('my_textarea', '', $post_id);
    	
    };
    add_action('acf/save_post', 'add_repeater_row_on_save', 20);
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.