Created
October 17, 2019 04:46
-
-
Save alexwcoleman/dadc9d2f54b5d4883258b75046944880 to your computer and use it in GitHub Desktop.
CMB2 to ACF Repeater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make sure to comment out add_action when you're done, or working on the data | |
add_action( 'init', 'cmb2_to_acf' ); | |
function cmb2_to_acf() { | |
// You can use offset if needed. | |
$args = array( | |
'post_type' => 'some_post_type', | |
'numberposts' => 400, // whatever number. | |
// 'offset' => 800, | |
); | |
$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); | |
// CMB2 data is serialized. Well, at least mine was ;) | |
$cmb_array = unserialize($meta['_some_cmb2_group'][0]); | |
// I used the ACF Repeater keys here, but didn't need it for the subfield below. | |
if( is_array( $cmb_array ) ) { | |
$repeater_field_key = 'field_5da77d6327885'; | |
$title_key = 'field_5da77dfb27886'; | |
$button_key = 'field_5da78013c748f'; | |
$file_upload_key = 'field_5da77e0427887'; | |
$content_key = 'field_5da77e5c27888'; | |
$value_array = []; | |
$counter = 0; | |
foreach ( $cmb_array as $data ) { | |
$value_array[ $counter ] = array( | |
'section_title' => $data['description'], | |
'file_upload' => $data['file_id'], | |
'button_text' => $data['file_text'], | |
// Not sure if html_entity_decode is needed, but it worked: https://stackoverflow.com/a/57491553/1843781 | |
'additional_content' => html_entity_decode($data['content']), | |
); | |
$counter++; | |
} | |
update_field( $repeater_field_key, $value_array, $post_id ); | |
} else { | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment