Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Last active October 20, 2021 16:34
Show Gist options
  • Save twentyfortysix/44274952e1791c571fa99487cffc0465 to your computer and use it in GitHub Desktop.
Save twentyfortysix/44274952e1791c571fa99487cffc0465 to your computer and use it in GitHub Desktop.
first function migrates the simple id to acf relation array, second cleans the data
<?php
function addMetaId2Relations($post_type, $meta_from, $meta_to){
$arr = [
'post_type' => $post_type,
'posts_per_page' => -1,
'fields' => 'ids'
];
$q = new WP_Query($arr);
echo 'poluted:<br><br>';
foreach ($q->posts as $id) {
$value_to_migrate = get_field($meta_from, $id, false);
$relationValue = get_field($meta_to, $id, false);
$relationValue[] = $value_to_migrate;
update_field($meta_to, $relationValue, $id);
echo $id.'<br>';
}
}
function deleteMetaData($post_type, $meta_to_delete){
$arr = [
'post_type' => $post_type,
'posts_per_page' => -1,
'fields' => 'ids'
];
$q = new WP_Query($arr);
echo 'deleted:<br><br>';
foreach ($q->posts as $id) {
delete_field($meta_to_delete, $id);
echo $id. '<br>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment