Support

Account

Home Forums Add-ons Repeater Field Iterating a repeater field in acf/save_post

Solved

Iterating a repeater field in acf/save_post

  • Hi,

    I have a custom field group containing an ACF group field, and under this group field an ACF repeater field called “person_relationships”. This custom field group is connected to a custom post type called “person”.

    I have a frontend screen that allows the user to create a new custom post type. I would like to perform some action when saving the post.

    I am using the acf/save_post action and trying to access the repeater field. However for some reason I cannot access the content of the repeater field:

    
    add_action('acf/save_post', 'my_acf_save_post', 20);
    function my_acf_save_post($post_id)
    {
    	$post_type = get_post_type($post_id);
    	if ($post_type == 'person')
    	{
    		$person_relationships = get_field('person_relationships', $post_id);
    		while (have_rows($person_relationships)) : the_row();
    			// Do something
    		endwhile;
    	}
    }
    

    It seems the repeater field does not have any rows, although the user created rows in the frontend screen and these rows are indeed created in the post.

    Am I using the wrong action ? I will appreciate any assistance or guidance.

    Thanks,

    Amir.

  • I did some more investigations and it seems to me this is related to a problem with have_rows not working properly on the repeater field.

    It seems that other users are reporting similar issues, for example here:

    https://support.advancedcustomfields.com/forums/topic/if-have_rows-not-working/page/2/#post-73258

    Is anyone aware of a solution and/or workaround ?

  • this is incorrect

    
    /*
      This first line gets the entire repeater and store it in an array
      If you are going to use have_rows() then this is unnecessary 
    */
    $person_relationships = get_field('person_relationships', $post_id);
    
    /*
      With the above $person_relationships
      holds an array of rows and not a field name
    */
    		while (have_rows($person_relationships)) : the_row();
    			// Do something
    		endwhile;
    

    What you need to do is just the loop and not the first part

    
    if (have_rows(''person_relationships', $post_id)) {
      while (have_rows(''person_relationships', $post_id)) {
        the_row();
        // do something
      }
    }
    
  • Hi John,

    Thanks for the reply, this seems to be working OK now.

    Amir.

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

The topic ‘Iterating a repeater field in acf/save_post’ is closed to new replies.