Support

Account

Home Forums ACF PRO Dynamically save repeater subfield on save post

Unread

Dynamically save repeater subfield on save post

  • Trying to save dynamically save 1 of my repeater children fields on the fly from an API and it doesn’t seem to work.

    I am defining my fields like so:

    
    <?php
    
    if ( function_exists( 'acf_add_local_field_group' ) ) :
    
    	acf_add_local_field_group(array(
    		'key' => 'group_56f96b966b629',
    		'title' => 'Post Fields',
    		'fields' => array(
    			array(
    				'key' => 'field_5703fade2d927',
    				'label' => 'Titles',
    				'name' => 'titles',
    				'type' => 'repeater',
    				'instructions' => '',
    				'required' => 0,
    				'conditional_logic' => 0,
    				'wrapper' => array(
    					'width' => '',
    					'class' => '',
    					'id' => '',
    				),
    				'collapsed' => '',
    				'min' => 0,
    				'max' => 0,
    				'layout' => 'table',
    				'button_label' => 'Link New Title',
    				'sub_fields' => array(
    					array(
    						'key' => 'field_5703fb482d928',
    						'label' => 'Main ID',
    						'name' => 'main_id',
    						'type' => 'text',
    						'instructions' => '',
    						'required' => 1,
    						'conditional_logic' => 0,
    						'wrapper' => array(
    							'width' => '',
    							'class' => '',
    							'id' => '',
    						),
    						'default_value' => '',
    						'placeholder' => '',
    						'prepend' => '',
    						'append' => '',
    						'maxlength' => 17,
    						'readonly' => 0,
    						'disabled' => 0,
    					),
    					array(
    						'key' => 'field_57fasdffb482d928',
    						'label' => 'Secondary ID',
    						'name' => 'secondary_id',
    						'type' => 'text',
    						'instructions' => '',
    						'required' => 1,
    						'conditional_logic' => 0,
    						'wrapper' => array(
    							'width' => '',
    							'class' => '',
    							'id' => '',
    						),
    						'default_value' => '',
    						'placeholder' => '',
    						'prepend' => '',
    						'append' => '',
    						'maxlength' => 17,
    						'readonly' => 0,
    						'disabled' => 0,
    					),
    					
    				),
    			),
    			
    		),
    		'location' => array(
    			array(
    				array(
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'post',
    				),
    			),
    		),
    		'menu_order' => 0,
    		'position' => 'normal',
    		'style' => 'seamless',
    		'label_placement' => 'top',
    		'instruction_placement' => 'label',
    		'hide_on_screen' => '',
    		'active' => 1,
    		'description' => '',
    	));
    
    endif;
    

    I have a repeater with the name of titles that has subfields (both text), main_id and secondary_id

    The typical workflow will be a user will enter in many of these entries, but only enter in the main_id they usually do not know the secondary_id but we have an internal API call that can get the secondary_id from the main id — so the idea is to hook into the save post mechanism, make the API call and set the secondary_id on the fly.

    I wrote the following:

    add_action( 'acf/update_value/name=titles', 'save_secondary_id', PHP_INT_MAX, 3 );
    
    function save_secondary_id( $value, $post_id, $field ) {
    
    	foreach ( $value as $repeater ) {
    		$new_array                 = $repeater;
    		$secondary_id              = make_external_call_to_get_secondary_id_from_main_id( $repeater['main_id'] ); // api call completely hidden from ACF logic
    		$new_array['secondary_id'] = $secondary_id;
    
    	}
    
    	return $new_array;
    }
    
    

    At the point that save_secondary_id fires, the $value variable is 1 — it’s never the actual repeater value.

    Am I using the wrong hook or doing it wrong?

    I would typically hook into the acf/load_value hook or one of its variants – but the API call is rather expensive – it’s much more efficient to make that call and have the editors incur that expensive time consuming lag and not make the API call (or even cache the API call) on the frontend, since it’s unnecssary as the main_id will never change.

    Can you please advise?

Viewing 1 post (of 1 total)

The topic ‘Dynamically save repeater subfield on save post’ is closed to new replies.