Support

Account

Home Forums Add-ons Repeater Field Copy ALL repeater subfields to Meta Key Reply To: Copy ALL repeater subfields to Meta Key

  • That looks like my work you’ve copied. It looks like you want to copy all of the sizes in each row to a single value? I’m not exactly sure why you’d want to do that. Instead of adding each value you’d need to collect them and then add them all at once, something like this.

    
    add_action('acf/save_post', 'convert_repeater_to_wp', 20);
    function convert_repeater_to_wp($post_id) {
      // create a meta key to store the new values under
      $meta_key = 'combined_sizes_repeater' ;
      // clear all of the existing values for the field before adding new values
      // to make sure it's update correctly
      delete_post_meta($post_id, $meta_key);
      // now we add the values of the repeater to this field
      // an array to hold all the values
      $value = array();
      if (have_rows('gallery', $post_id)) {
        while(have_rows('gallery', $post_id)) {
          the_row();
          // add value to array
          $value[] = get_sub_field('size').'-';
          // don't add it here
          //add_post_meta($post_id, $meta_key, $value, false);
          // the last value "false" tells WP that this meta key can have multiple values
        } // end while have rows
        // after collecting all the values then add them to one meta value
        add_post_meta($post_id, $meta_key, implode('-', $value), false);
      } // end if have rows
    
    } // end function