Home › Forums › Add-ons › Repeater Field › Using acf_form() to store/fetch a multidimensional array meta_key › Reply To: Using acf_form() to store/fetch a multidimensional array meta_key
The first thing you’re going to want to do is take the filters out of your template and put them in your functions.php file.
With the additional information about the fields and arrays I think the following should make it work.
<?php
add_filter('acf/load_value/name=my_repeater_field','load_my_repeater_field_value', 10, 3);
function load_my_repeater_field_value($value, $post_id, $field) {
$load_value = maybe_unserialize(get_post_meta($post_id, '_wc_booking_availability', true));
if (is_array($load_value)) {
// this will remove the extra array wrapper
$load_value = $load_value[0];
}
if (is_array($load_value)) {
// this is seperate
// just in case the above does not return a nested array
// you need to format some of the values differently for acf
// acf stores dates without the -
foreach ($load_value as $index => $row) {
$load_value[$index]['from'] = str_replace('-', '', $load_value[$index]['from']);
$load_value[$index]['to'] = str_replace('-', '', $load_value[$index]['to']);
}
}
return $load_value;
};
add_filter('acf/save_post', 'update_my_repeater_field_value', 20);
function update_my_repeater_field_value($post_id) {
// check the post type
// you don't want to do this on every post type
if (get_post_type($post_id) != 'product') {
return;
}
$value = array(get_field('my_repeater_field', $post_id));
update_post_meta($post_id, '_wc_booking_availability', $value);
}
?>
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.