Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created February 9, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustardBees/6f36075c081e5c0ec28530327e8ade32 to your computer and use it in GitHub Desktop.
Save mustardBees/6f36075c081e5c0ec28530327e8ade32 to your computer and use it in GitHub Desktop.
Convert file_list to CMB2 repeatable field groups. Go through post meta by a specified key. Convert file_list data to a CMB2 repeatable field group.
<?php
/*
Plugin Name: Convert file_list to CMB2 repeatable field groups
Plugin URI: https://www.iweb.co.uk/
Description: Go through post meta by a specified key. Convert file_list data to a CMB2 repeatable field group.
Version: 1.0.0
Author: iWeb
Author URI: https://www.iweb.co.uk/
*/
if ( isset( $_GET['convert_file_list'] ) ) {
add_action( 'template_redirect', function () {
global $wpdb;
$results = $wpdb->get_results(
"
SELECT pm.post_id, pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '_cmb_banners'
"
);
if ( empty( $results ) ) {
die( 'No post meta with that key.' );
}
foreach ( $results as $result ) {
printf( 'Working on post ID %s:<br>', $result->post_id );
$meta_value = maybe_unserialize( $result->meta_value );
$new_format = array();
foreach ( $meta_value as $key => $value ) {
$new_format[] = array(
'image_id' => $key,
'image' => $value,
'url' => '',
'caption' => '',
);
}
update_post_meta( $result->post_id, '_cmb_banners', $new_format, $meta_value );
}
die( 'Done.' );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment