Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created July 22, 2016 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustardBees/e45fed6a74aba6dd103aa9cc6e740d7c to your computer and use it in GitHub Desktop.
Save mustardBees/e45fed6a74aba6dd103aa9cc6e740d7c to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Convert SMURF to CMB2 repeatable field groups
Plugin URI: http://www.iweb.co.uk/
Description: Go through and zero index each SMURF array.
Version: 1.0.0
Author: iWeb
Author URI: http://www.iweb.co.uk/
*/
if ( isset( $_GET['fix_smurf'] ) ) {
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_specifications'
"
);
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 );
$fixed_meta_value = array_values( $meta_value );
update_post_meta( $result->post_id, '_cmb_specifications', $fixed_meta_value, $meta_value );
}
die( 'Done.' );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment