Last active
April 6, 2020 07:58
-
-
Save kkarpieszuk/dc3892fcbded4e3980d6976d24fbc476 to your computer and use it in GitHub Desktop.
How to fix malformed data in ACF field, caused by ACFML 1.6.0
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
// edit: if you are using wp-cli, you can try to use this plugin https://github.com/OnTheGoSystems/acfml_fixer | |
// otherwise, please follow instructions below | |
// PLEASE READ ALL COMMENTS BEFORE RUNNING THIS SNIPPET | |
// ALSO THE COMMENTS AT THE END OF THIS SNIPPET | |
// | |
// CREATE DATABASE BACKUP! | |
// | |
// update ACFML to the version 1.6.1, you can find it in wpml.org > my account > downloads | |
// add the whole snippet to your functions.php in theme directory and visit any page of your blog/site | |
function fix_json_like_fields( $start_id = 0, $end_id = 1000 ) { | |
global $wpdb; | |
$query = $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE post_id > %d AND post_id <= %d", $start_id, $end_id ); | |
$metas = $wpdb->get_results( $query ); | |
foreach ( $metas as $meta ) { | |
if ( strpos( $meta->meta_key, "_" ) === 0 ) { | |
continue; | |
} | |
$meta_value = maybe_unserialize( $meta->meta_value ); | |
if ( is_array( $meta_value ) && isset( $meta_value[ $meta->meta_key ] ) ) { | |
update_post_meta( $meta->post_id, $meta->meta_key, array_pop( $meta_value[ $meta->meta_key ] ) ); | |
} | |
} | |
} | |
// call this function: | |
fix_json_like_fields(); | |
// the function by default fixes data in post with id between 1 and 1000 | |
// if you have more posts, change function to: | |
// fix_json_like_fields( 1000, 2000); | |
// and call again and repeat for next group of posts as long as all data will not be fixed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment