Support

Account

Home Forums ACF PRO update repeater field on acf/save_post

Solving

update repeater field on acf/save_post

  • We have a custom post type that has several fields that are created with ACF. We also have a couple of different “repeaters” (sub-fields) on it as well. One of the repeaters is the location’s address information and we need to do geolocation first and then update the lat/long field.

    I am able to loop through the submitted form data but so far I haven’t yet found the way to get the repeater’s “updated form field” into the database yet.

    Here is my existing code in functions.php:

    function post_updating_callback($post_id){
    	
    	// Ensure we have ACF fields on the post.
    	if( empty($_POST['acf']) ) { return; }
    
    	$locations = $_POST['acf']['field_55282e0aa0a6f'];
    
    	// get address info to do geolocation
    
    	foreach ($locations as $key => $subValue) {
    		// sample field update
    		$locations[$key]['field_552b2a87ecdbc'] = 'postSave '.$key;
    		
    		// or even this way
    		$subValue['field_552b2a87ecdbc'] = 'postSave '.$key;
    
    		// different attempts to save it
    		// update_sub_field($locations[$key]['field_552b2a87ecdbc'], 'postSave '.$key);
    		// update_field('field_552b2a87ecdbc', 'postSave {$key}', $post_id);
    		// update_sub_field('field_552b2a87ecdbc', 'postSave {$key}', $post_id);
    	}
    
    	return;
    }
    add_action('acf/save_post', 'post_updating_callback', 20);

    I also tried to use the have_rows() like:

    if( have_rows($locations) ) {
    	$i = 0;
    	echo 'have rows!';
    
    	while( have_rows($locations) ) {
    		echo 'inside while;';
    	}
    }

    The have_rows() only throws errors for me as well — maybe supposed to only be used on the front-end versus on in the functions.php file?

    Thoughts or ideas?

    Thanks,
    Jeremy

  • Usually, when I’m doing something like this with repeater fields I don’t actually use ACF functions or try to work directly with the values in $_POST.

    Here’s an example of something I’d do, I do this because I know how ACF stores data and it’s just easier and faster to work with native WP functions for me.

    
    post_updating_callback($post_id) {
      $repeater = 'my_repeater'; // the field name of the repeater field
      $subfield1 = 'subfield_1'; // the field I want to get
      $subfield2 = 'subfield_2'; // the field I want to set
      
      // get the number of rows in the repeater
      $count = intval(get_post_meta($post_id, $repeater, true);
      // loop through the rows
      for (i=0; $i<$count; $i++) {
        $get_field = $repeater.'_'.$i.'_'.$subfield1;
        $set_field = $repeater.'_'.$i.'_'.$subfield2;
        $value = get_post_meta($post_id, $get_field, true);
        // do whatever I need to calculate the value to set
        // $new_value will hold the value to set
        update_post_meta($post_id, $set_field, $new_value);
      }
    }
    

    Hope this helps.

  • @John Huebner:
    Thanks for your code! This works great, however there were 2 minor syntax errors in your code. Updated code:

    function post_updating_callback( $post_id ) {
      $repeater  = 'page_sections'; // the field name of the repeater field
      $subfield1 = 'page_section_title'; // the field I want to get
      $subfield2 = 'page_section_slug'; // the field I want to set
      // get the number of rows in the repeater
      $count = intval(get_post_meta($post_id, $repeater, true));
      // loop through the rows
      for ($i=0; $i<$count; $i++) {
        $get_field = $repeater.'_'.$i.'_'.$subfield1;
        $set_field = $repeater.'_'.$i.'_'.$subfield2;
        $value     = get_post_meta($post_id, $get_field, true);
        $new_value = sanitize_title($value);
        update_post_meta($post_id, $set_field, $new_value);
      }
    }
    add_action('acf/save_post', 'post_updating_callback', 20);

    Thanks a lot!

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

The topic ‘update repeater field on acf/save_post’ is closed to new replies.