Support

Account

Home Forums Add-ons Repeater Field Repeater field with many inputs Reply To: Repeater field with many inputs

  • Thanks John for your answer.

    Sorry for the lack of information from my side.

    Firstly in the hook filter acf/prepare_field, I change the field name:

    add_filter( 'acf/prepare_field/name=MY_FIELD', function ( $field ) {
    
    	$field['name'] = 'acf[' . $field['key'] . '-clone]';
    
    	return $field;
    	
    }, 10, 1 );

    Secondly to get the value saved in my hidden field (used to save the data as a JSON) I’m using the hook filter acf/load_value:

    add_filter( 'acf/load_value/name=MY_FIELD', function ( $value, $post_id, $field ) {
    
    	$field = acf_get_field( $field['name'] );
    
    	$json = get_field( $field['name'] . '-json', $post_id );
    
    	// $data proccessed here
    
    	return $data;
    
    }, 10, 3 );

    Finally, I’m using the hook acf/save_post to update my JSON field:

    add_filter( 'acf/save_post', function ( $post_id ) {
    
    	$field_name = 'MY_FIELD';
    
    	if ( $field_name ) {
    
    		$field = false;
    
    		foreach ( array_keys( $_POST['acf'] ) as $k ) {
    
    			if ( ! str_contains( $k, '-clone' ) ) {
    				continue;
    			}
    
    			$field = get_field_object( str_replace( '-clone', '', $k ), $post_id );
    
    			if ( $field['name'] === $field_name ) {
    				break;
    			}
    
    		}
    
    		if ( $field ) {
    
    			$data = $_POST['acf'][ $field['key'] . '-clone' ];
    
    			update_field( $field['name'] . '-json', json_encode( $data ), $post_id );
    
    		}
    
    	}
    
    }, 10, 1 );

    To give you more context, my repeater is in a custom post type. For each post I’ve this repeater field containing a list of 250 countries. For each country I need to have 12 fields to get specific data from them for this post.