Created
October 8, 2019 21:19
-
-
Save alexwcoleman/bd1fbf076b99288505bd10ab15700529 to your computer and use it in GitHub Desktop.
Moving WooCommerce Images to ACF Repeater subfields
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( 'init', 'test_update_products' ); | |
function test_update_products() { | |
// You can use offset if needed. | |
$args = array( | |
'post_type' => 'my_awesome_cpt', | |
'numberposts' => 300, | |
//'offset' => 1000, | |
); | |
$my_posts = get_posts( $args ); | |
foreach ( $my_posts as $my_post ) { | |
my_acf_save_post( $my_post->ID ); | |
} | |
} | |
function my_acf_save_post( $post_id ) { | |
// is used the ACF Repeater keys here, but didn't need it for the subfield below. | |
$repeater_field_key = 'field_5d95181ce7407'; | |
$sub_field_key = 'field_5d951835e7408'; | |
$media = get_attached_media( 'image', $post_id ); | |
$value_array = []; | |
$counter = 0; | |
foreach ( $media as $m ) { | |
$value_array[ $counter ] = array( | |
'gallery_image' => $m->ID, | |
); | |
$counter++; | |
} | |
update_field( $repeater_field_key, $value_array, $post_id ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment