Skip to content

Instantly share code, notes, and snippets.

@drewbaker
Last active October 12, 2023 20:34
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 drewbaker/6da547718f052fe9447f3cd4c930b09c to your computer and use it in GitHub Desktop.
Save drewbaker/6da547718f052fe9447f3cd4c930b09c to your computer and use it in GitHub Desktop.
A PHP script as a starting point to convert old WP meta fields to ACF meta fields. Add this to theme's functions.php file.
/*
* Use this script as a starting point to convert old WP meta fields to ACF meta fields.
* Add ?convert_meta to the URL and load any page in backend.
* You should edit the script, and only run it once.
*/
function custom_convert_meta_to_acf()
{
// Abort if no query param in URL
if(! isset($_GET["convert_meta"])) {
return;
}
// Get all pages/posts/cpts
$args = array(
'post_type' => 'any',
'posts_per_page' => -1,
);
$posts = get_posts($args);
$output = [];
foreach($posts as $post) {
// Update selected meta fields
// See: https://www.advancedcustomfields.com/resources/update_field/
if($post->_custom_credits) {
update_field("field_60cbeab35135e", $post->_custom_credits, $post->ID);
}
if($post->_custom_video_url) {
update_field("field_60cbeabc5135f", $post->_custom_video_url, $post->ID);
}
$output[] = $post->ID;
}
var_dump("These posts were updated:", $output);
}
//add_action("admin_init", "custom_convert_meta_to_acf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment