Support

Account

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 only thing that I can think of is that the original field may not have a value at some point that that’s messing it up. I added some more error checking to the first array.

    If this this doesn’t help then you’ll need to start dumping some values to the screen at each point to see what’s going on.

    
    <?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)) {
          return $value;
        }
        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) {
            if (count($row)) {
              $load_value[$index]['from'] = str_replace('-', '', $load_value[$index]['from']);
              $load_value[$index]['to'] = str_replace('-', '', $load_value[$index]['to']);
            } // end if count row
          } // end foreach row
        } // end if is array
        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);
      }
    
    ?>