I have the following code in my functions.php file that I'm using to create a custom meta_key called 'spec_wp' (which is based on the value of an ACF repeater):
add_filter('acf/save_post', 'convert_spec_to_standard_wp_meta', 20);
function convert_spec_to_standard_wp_meta($post_id) {
$meta_key = 'spec_wp';
delete_post_meta($post_id, $meta_key);
$saved_values = array();
if (have_rows('specs', $post_id)) {
while (have_rows('specs', $post_id)) {
the_row();
$specval = get_sub_field('spec_value');
if (isset($saved_values[$specval])) {
continue;
}
add_post_meta($post_id, $meta_key, $specval, false);
$saved_values[$specval] = $specval;
}
}
}
The code works as intended, but spec_wp is only created for a post when that post is updated from its edit screen.
Does anyone know of a way to bulk-update posts so that I don't have to go through and update them one by one? I have 1,700 posts that need this meta_key, so updating them all manually isn't really an option.
I've tried using the 'bulk actions' > 'edit' > 'update' feature in on the 'all posts' page but it doesn't seem to work.
I've also tried changing the action to 'acf/init' and just 'init', but that didn't work either.