Skip to content

Instantly share code, notes, and snippets.

@gthayer
Last active November 11, 2022 20:06
Show Gist options
  • Save gthayer/0d71df7cb325549cc37661f1c9378fd9 to your computer and use it in GitHub Desktop.
Save gthayer/0d71df7cb325549cc37661f1c9378fd9 to your computer and use it in GitHub Desktop.
//add_action('wp_head', 'update_meta_values');
function update_meta_values() {
/*
* Takes a set of fields and resets them as part of a repeater
* To start, create a new repeater field and enter the slug in $new_repeater
* Find the fields you would like to move in wp_posts and set the post_parent to the new repeater's ID
*/
// Slug of the new repeater
$new_repeater = 'repeater_field';
// Set the fields you would like to move's slugs in $rows
$rows = array(
'foo',
'bar'
);
// Query the effected posts
$args = array(
'post_type' => array( 'cpt_post_type' ),
'posts_per_page' => -1
);
$posts = get_posts($args);
foreach ($posts as $post) {
// Update Meta Fields
$id = $post->ID;
$meta = get_post_meta( $id );
unset($updated);
// Loop each posts Meta values
foreach ( $meta as $key => $value ) {
// Loop the possible fields
foreach ( $rows as $row ) {
//Check if field name is in meta value
if ( strpos( $key, $row ) !== false ) {
// Grab the value's name, prepend it with the repeater name.
$new_post_meta = str_replace( $row, $new_repeater . '_0_' . $row , $key );
// Add new meta
update_post_meta( $id, $new_post_meta, $value[0] );
// Delete old meta
delete_post_meta( $id, $key );
$updated = true;
break;
}
}
}
// If this post has repeater rows, mark it with 1
if ($updated) {
update_post_meta( $id, 'resource_list', 1 );
update_post_meta( $id, '_resource_list', 'field_574ee7028384d' );
}
}
}
@kodie
Copy link

kodie commented Nov 11, 2022

@gthayer Thank you for this. I ended up taking your code and running with it for a plugin/WP CLI command: https://github.com/kodie/migrate-acf-field-data-to-repeater

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment