Last active
November 11, 2022 20:06
-
-
Save gthayer/0d71df7cb325549cc37661f1c9378fd9 to your computer and use it in GitHub Desktop.
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
//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' ); | |
} | |
} | |
} |
Thank you for this hack
I found how to do it directly in the back office
First you have to add a subfield to the repeater field and save
Then drag the desired field and drop it in the sub-fields block of the repeater
@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
@webdev2178 - I believe you need to define the repeater field in ACF before running this script. This script is for the migration of the field values, not the field settings themselves.
However, to be honest, I've not used ACF in about 2 years and I don't have a strong memory of this exact use case.
You might also want to look into
field_574ee7028384d
. I don't remember why that was set, but my guess is it was specific to my case.