Home › Forums › Add-ons › Repeater Field › Convert raw repeater data back into an array
I need to get the data stored in a block’s repeater field that is located in a different post. I’m able to do the first part using
parse_blocks() to locate the specific block and get its raw data. The problem I’m having is that the repeater data is stored in its flat format and I need to get it back into an array so I can use it.
I’ve been trying all kinds of functions that seem to be part of a solution but none have worked. The closest I’ve gotten so far is calling
acf_setup_meta( $block['attrs']['data'] ) then I can use
acf_get_meta_field to get the repeater field’s settings which I can pass to
acf_get_field_type( 'repeater' )->load_field(), but that’s as far as I’ve gotten.
Anyone know how to do this?
what does the data that needs to be converted actually look like?
@hube2 Here’s what the block’s data looks like from
Array ( [blockName] => site/recipes [attrs] => Array ( [name] => site/recipes [data] => Array ( [heading_level] => h2 [_heading_level] => blocks-recipes-heading_level [items_per_page] => 9 [_items_per_page] => blocks-recipes-items_per_page [show_pagination] => 1 [_show_pagination] => blocks-recipes-show_pagination [filter_categories_0_category] => easy_meal_categories [_filter_categories_0_category] => blocks-recipes-filter_categories-category [filter_categories_0_easy_meal_categories-value] => 30-minutes-or-less [_filter_categories_0_easy_meal_categories-value] => blocks-recipes-filter_categories-easy_meal_categories-value [filter_categories_1_category] => consumer_symbols [_filter_categories_1_category] => blocks-recipes-filter_categories-category [filter_categories_1_consumer_symbols-value] => dairy-free [_filter_categories_1_consumer_symbols-value] => blocks-recipes-filter_categories-consumer_symbols-value [filter_categories] => 2 [_filter_categories] => blocks-recipes-filter_categories ) [mode] => preview ) [innerBlocks] => Array () [innerHTML] => EMPTY STRING [innerContent] => Array () )
filter_categories is a repeater containing a dropdown to select the category and another dropdown to select the value. Each category has its own set of values so there’s a conditional on each value dropdown to match the selected category. That’s why for the value the category name is a part of the key
filter_categories_0_<category>-value. In the above code, there are 2 filters selected,
easy_meal_categories = 30-minutes-or-less and
consumer_symbols = dairy-free.
I realize I could do this the hard way and parse the keys out, but that’s brittle and especially difficult if repeaters are nested.
That’s interesting and it will not be easy to convert that.
this holds the field value
this holds the ACF field key reference
In this case
I am assuming this is constructed like
block- simply tells ACF that this field belongs to a block.
On to the repeater
[filter_categories_0_category] the ‘category’ field from the 1st row of the the ‘filter_categories’ repeater. This index is constructed like this
In order to build an array you would need to loop over the data and parse the array index and create what ACF usually stores in an array. You would also need to format the values.
Going to be honest, I have no idea how you’d do this, especially considering that you have multiple possible secondary sub fields dependent of the value of the first sub field.
Thanks to ACF support’s help, this has been solved!
acf_setup_meta( $block['attrs']['data'], acf_get_block_id( $block['attrs']['data'] ), true ); get_field( 'filter_categories' );
true in the call to
acf_setup_meta() means it’s replacing the main meta data, which you might not want to do if you still need to access it. You can do it this way to preserve it:
$block_id = acf_get_block_id( $block['attrs']['data'] ); acf_setup_meta( $block['attrs']['data'], $block_id ); get_field( 'filter_categories', $block_id );
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.