Support

Account

Home Forums General Issues Updating Sub Field of Group Inside a Repeater

Solved

Updating Sub Field of Group Inside a Repeater

  • OK, I’m going a bit mad on this one. This is the scenario:

    +Repeater -> r1
    +Group -> g1
    +TextField -> t1

    I have created an action to change t1 when a the form is saved. This is the code:

    	function my_check_for_change($value, $post_id, $field) {
    	// Check for repeater row
    		if( have_rows('r1') ): ?>
                <?php while( have_rows('r1')) : the_row() ?>
                    <?php
                        update_sub_field( array('r1', 1, 'g1', 1, 't1'), 'cow' );
                        update_sub_field( 't1', 1, 'cow');
                        update_sub_field( 'g1_t1', 1, 'cow');
                        update_sub_field( 'r1_g1_t1', 1, 'cow');													
    
    					}
    				?>
                <?php endwhile; ?>
            <?php endif;
    	}

    No matter the different combinations of update_sub_field, the field is NOT updated at all. Any help is greatly appreciated.

  • I noticed an extra closing curly bracket. Not sure if that is it.

    I think your group field is like a repeater. I haven’t work with this field type, so I’d be guessing. I think you have to put if/while for your group inside your first repeater.

    The documentation is here:
    Group

    In other words, the group is nested in the repeater.
    I think the code should look something like this:

    function my_check_for_change($value, $post_id, $field) {
    
    		if( have_rows('r1') ): 
    			while( have_rows('r1')) : the_row() 
    			
    				if( have_rows('g1') ): 
    					while( have_rows('g1')) : the_row() 
    		
                        	update_sub_field( array('r1', 1, 'g1', 1, 't1'), 'cow' );
                        	update_sub_field( 't1', 1, 'cow');
                        	update_sub_field( 'g1_t1', 1, 'cow');
                        	update_sub_field( 'r1_g1_t1', 1, 'cow');													
    
    					endwhile; 	
            		endif;
    			endwhile; 	
            endif;
    
    	}
  • Thanks bosoxbill, will give it a shot and report back!

  • To answer my question and assuming you are changing the first row, you need to simply use update_field:

    update_field(r1_0_g1_t1, ‘cow’, get_the_ID());

  • It was this answer that finally answered my question on how to save to fields within a Group field – basically, using an underscore reference method.

    I didn’t see that at all in the documentation, which seems more geared toward saving fields within a repeater.

    Also, ACF’s Edit Field Group constructor “Underscores and dashes allowed”. Given that this method depends on underscore separation, is the tip correct?

  • Albeit, when I do…

    update_field('categories_sector', 'My Value', $org_id_prefixed);

    … I am now getting the following …

    Warning: Invalid argument supplied for foreach() in /home/myfolder/public_html/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-checkbox.php on line 490

    But only when “Save Custom” for the Checkbox named “Sector” is switched ON.

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

The topic ‘Updating Sub Field of Group Inside a Repeater’ is closed to new replies.