Skip to content

Instantly share code, notes, and snippets.

@gthayer
Last active July 1, 2017 13:52
Show Gist options
  • Save gthayer/6c1b8601dc2e4d00b966 to your computer and use it in GitHub Desktop.
Save gthayer/6c1b8601dc2e4d00b966 to your computer and use it in GitHub Desktop.
function change_acf_meta_keys() {
global $wpdb;
$values = array(
"0" => array(
'old_value' => 'foo1',
'new_value' => 'bar1',
),
"1" => array(
'old_value' => '_foo1',
'new_value' => '_bar1',
),
"2" => array(
'old_value' => 'foo2',
'new_value' => 'bar2',
),
"3" => array(
'old_value' => '_foo2',
'new_value' => '_bar2',
)
);
foreach ($values as $value) {
$old_value = $value['old_value'];
$new_value = $value['new_value'];
$querystr = "
SELECT $wpdb->postmeta.*
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key REGEXP '$old_value'";
$wpdb->show_errors();
$metas = $wpdb->get_results($querystr, OBJECT);
foreach ($metas as $meta) {
$key = $meta->meta_key;
$key = str_replace($old_value, $new_value, $key);
$wpdb->query(
$wpdb->prepare(
"
UPDATE $wpdb->postmeta
SET meta_key = %s
WHERE meta_id = %d
", $key, $meta->meta_id
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment