Skip to content

Instantly share code, notes, and snippets.

@alexwcoleman
Created October 25, 2019 04:51
Show Gist options
  • Save alexwcoleman/f04d649b4c10e5d8cf0ec7b2932bd5f7 to your computer and use it in GitHub Desktop.
Save alexwcoleman/f04d649b4c10e5d8cf0ec7b2932bd5f7 to your computer and use it in GitHub Desktop.
CMB2 Image Repeater to ACF Pro Repeater
// PLEASE NOTE!
// I set my ACF Image Repeater to return only the image ID...
// So this may not work if you have it set to return an Object or...?
// I found this helpful: https://support.advancedcustomfields.com/forums/topic/repeater-update_field/
add_action( 'init', 'cmb2_to_acf' );
function cmb2_to_acf() {
// You can use offset if needed.
$args = array(
'post_type' => 'a_custom_post_type',
'numberposts' => 100, // whatever number.
);
$my_posts = get_posts( $args );
foreach ( $my_posts as $my_post ) {
my_acf_save_post( $my_post->ID );
}
}
function my_acf_save_post( $post_id ) {
$meta = get_post_meta( $post_id );
$field_key = 'field_5db205dacf56f';
$data = unserialize( $meta['some_image_repeat'][0] ); // This is CMB2
$value_array = [];
$counter = 0;
foreach ( $data as $d ) {
$value_array[ $counter ] = array(
// d['image_id'] is the image ID
'field_5db205eecf570' => $d['image_id'],
);
$counter++;
}
update_field( $field_key, $value_array, $post_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment