Last active
October 12, 2023 20:34
-
-
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.
This file contains hidden or 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
/* | |
* 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