Created
February 9, 2017 11:20
-
-
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.
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
<?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