Home Forums Gutenberg Prepopulating ACF Block Repeater Field values from Custom Fields


Prepopulating ACF Block Repeater Field values from Custom Fields

  • Hi,

    I’m trying to do the following:

    I’ve got hundreds of posts with ACF Nested Repeater Fields that I’m now trying to replicate with an ACF Block. As there are hundreds of them, it would be quicker to prepopulate the ACF Block fields from the already filled fields, as they are the same, and I thought this would be possible with load_value but I’m having a hard time understanding how it works.

    First, is it possible to load the values from fields into an ACF Block? I’ve succeeded with putting the post_id manually but when I use $post_id does not work. I’ve seen acf blocks are not saved in the post meta, so how do I get $post_id from inside the block?

    Second problem is with Nested Repeaters. I’ve succeeded in retrieving the values (with post_id added manually) but I just only get the last value added so I’m obviously not looping correctly through the repeaters.

    There is a repeater field (‘ingredients’) which has two sub fields (‘header’ is a text field and ‘ingredients_list’ is a repeater field). The nested repeater field ‘ingredients_list’ has a text field ‘ingredient’. There could be several headers and each header has several ingredients, but all I’m getting is the last header and the last ingredient added. This works perfectly in the front end but I can’t seem to get it work inside the ACF Block.

    The code I’ve been playing around is this:

    add_filter('acf/load_value/key=field_5c197b8a07677',  'acf_load_default', 10, 3);
    function acf_load_default($value, $post_id, $field) {
      if ($value === false) {
    		if( have_rows('ingredients', 1) ):
    	            while( have_rows('ingredients', 1) ): the_row(); 		
    		       $header = get_sub_field('header', 1);	 																	 
                              if( have_rows('ingredients_list', 1) ): 
    				while( have_rows('ingredients_list', 1) ): the_row();
    	                          $ingredient = get_sub_field('ingredient', 1);
    				  $value = array();
        				  $value[] = array(									 
                                       'field_5cb06077469b7' => "$header",									  
                                       'field_5cb06094469b8' => array(        									 
                                          array('field_5cb060b4469b9' => "$ingredient"),
          return $value;

    Any idea how to do it or if it is at all possible?


Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.