Home › Forums › Add-ons › Repeater Field › Copy ALL repeater subfields to Meta Key
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
The topic ‘Copy ALL repeater subfields to Meta Key’ is closed to new replies.
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.