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
Hi John,
Thanks for the response!
I’ve been fiddling with the code that you sent. The good news is with your code, I’m able to get empty rows to show in the repeater field. The not-so-good news is I’m still not able to pre-populate the form rows with values on the front end, nor post the values to the correct pre-existing field.
I’ve noticed that the pre-existing field (‘availability’ field, we’re using WooCommerce Bookings) is wrapping the item in an extra array, such as:
Array ( [0] => Array ( [0] => Array ( [type] => custom [bookable] => yes [priority] => 10 [from] => 2016-02-02 [to] => 2016-02-03 ) [1] => Array ( [type] => custom [bookable] => no [priority] => 10 [from] => 2016-02-10 [to] => 2016-02-12 ) [2] => Array ( [type] => custom [bookable] => no [priority] => 10 [from] => 2016-02-04 [to] => 2016-02-20 ) ) )
…whereas, the array output from ACF using your code is:
Array ( [0] => Array ( [type] => custom [bookable] => yes [priority] => 10 [from] => 2016-02-02 [to] => 2016-02-03 ) [1] => Array ( [type] => custom [bookable] => no [priority] => 10 [from] => 2016-02-10 [to] => 2016-02-12 ) [2] => Array ( [type] => custom [bookable] => no [priority] => 10 [from] => 2016-02-04 [to] => 2016-02-20 ) )
The code I’m using so far:
<?php
/**
* The template for displaying full width pages.
*
* Template Name: ACF Front End Repeater Form
*
* @package storefront
*/
?>
<?php $post_id = 55; ?>
<?php acf_form_head(); ?>
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?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) {
/*
--> adding the array() around get_post_meta() makes this output array match the default one (see line 57 below please), but it does not render form rows
$load_value = maybe_unserialize(get_post_meta($post_id, '_wc_booking_availability', true));
*/
$load_value = maybe_unserialize(get_post_meta($post_id, '_wc_booking_availability', true));
return $load_value;
/*
--> tried this, but it added extra empty rows somehow
return call_user_func_array('array_merge', $load_value);
*/
};
?>
<?php
$options = array(
'post_id' => $post_id,
'post_title' => true,
'field_groups' => array(
18,
),
'submit_value' => __("Update", 'acf'),
'updated_message' => __("Post updated", 'acf'),
'uploader' => 'wp'
);
?>
<?php acf_form($options); ?>
<?php
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 = get_field('my_repeater_field', $post_id);
update_post_meta($post_id, '_wc_booking_availability', $value);
} // when i submit the field, the ACF array (output below) deletes all the values
?>
<?php
// Printing both the pre-existing field and the ACF field arrays for debugging/testing
// ACF
$acf_repeater_array = get_field( "my_repeater_field", $post_id);
echo 'ACF repeater field array:<br><br>';
if( empty( $acf_repeater_array ) ){
echo '<strong>Empty</strong><br><br>';
} else {
print_r(array_values($acf_repeater_array)); // this doesn't match the array for the pre-existing field unless i wrap get_post_meta() in an array() on line 25
echo '<br><br><hr>';
};
// Pre-existing field
$pre_existing_array = get_post_meta( $post_id, '_wc_booking_availability' );
echo 'Pre-existing field array:<br><br>';
if( empty( $pre_existing_array ) ){
echo '<strong>Empty</strong><br><br>';
} else {
print_r(array_values($pre_existing_array));
echo '<br><br><hr>';
echo 'Pre-existing field array (when I \'flatten\' the pre-existing array):<br><br>';
$flat = call_user_func_array('array_merge', $pre_existing_array);
print_r(array_values($flat)); // this DOES match the array for the pre-existing field
echo '<br><br><hr>';
};
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
I’m at the point where I don’t know if I’m making progress or just getting more lost, so huge, huge thanks in advance for any insights!
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.