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"),
)
);
endwhile;
endif;
endwhile;
endif;
}
return $value;
}
Any idea how to do it or if it is at all possible?
Thanks
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.