Support

Account

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

Solved

Copy ALL repeater subfields to Meta Key

    • msabry

    • December 24, 2017 at 10:57 pm

    Hello ..

    I have a repeater field calld (Gallery) .. under this I have subfield called (size) ..

    after long search i have found this function to copy subfields to new meta key .

    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
      if (have_rows('gallery', $post_id)) {
        while(have_rows('gallery', $post_id)) {
    		the_row();
          $value = get_sub_field('size').'-';
          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
      } // end if have rows
    
    } // end function

    but this function only copy the (first) row “size” only ..
    and I want to copy (ALL) subfields (size) to “combined_sizes_repeater”

    can you help me with that ??

    thanks 🙂

  • 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
    
    • msabry

    • January 5, 2018 at 1:12 am

    @hube2 Sure its your work … again .. you saved the day 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.