Last active
June 16, 2017 21:27
-
-
Save jasoncomes/23a3bcdd1be22d20895491bb479df645 to your computer and use it in GitHub Desktop.
After removing ACF field groups from site, this function should be called once* on the fields that were trashed to remove them from WP Database.
This file contains hidden or 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
<?php | |
/** | |
* Remove ACF DB Meta | |
* | |
* After removing ACF field groups from site, this function should be called once* on the fields that were trashed to remove them from WP Database. | |
* Removes them from the postmeta & options tables in the database. | |
* Removes from all pages, posts, categories, taxonomies, users the field groups were attached too. | |
* Note: Should only be called and used on local development, please remove from site when you deploy | |
* | |
* @version 1.0.0 | |
* | |
* @param string/array $meta - Fields to remove from database | |
* @param string $prefix - If fields need additional prefixing that was originally setup on fields & for repeater prefixes | |
* @param string $table - DB Table to remove fields from(Note: fields are additionaly being removed from options table) | |
* @param string $table_field - DB Column field name | |
* @param string $table_value - DB Column field value | |
* | |
*/ | |
function removeACFDBMeta($meta, $prefix = false, $table = 'postmeta', $table_field = 'meta_key', $table_value = 'meta_value') | |
{ | |
global $wpdb; | |
$prefixes = array(); | |
// If no meta fields | |
if (empty($meta)) { | |
print_r('No Meta Fields Specified'); | |
return; | |
} | |
// If array of meta fields | |
if (is_array($meta)) { | |
// Loop through meta fields | |
foreach ($meta as $field => $value) { | |
// Single meta field | |
if (!is_array($value)) { | |
// meta_key(prefix is repeater parent field with child fields) | |
$meta_field = $prefix ? $prefix . '_' . $value : $value; | |
// Specified Table Removal | |
$results = $wpdb->query("DELETE FROM {$wpdb->$table} WHERE {$table_field} REGEXP '{$meta_field}$'"); | |
// Options Table Removal(Remove if not needed) | |
$option_results = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name REGEXP '{$meta_field}$'"); | |
$results += $option_results ? $option_results : 0; | |
// Print results | |
print_r($meta_field . ' - ' . $results . ' rows removed in ' . $wpdb->$table . ' and ' . $wpdb->options); | |
echo '<br />'; | |
} else { | |
// meta_key(prefix is repeater parent field with child fields) | |
$meta_field = $prefix ? $prefix . '_' . $field : $field; | |
// Repeater meta field | |
$max_repeater = $wpdb->get_var($wpdb->prepare("SELECT max({$table_value}) FROM {$wpdb->$table} WHERE {$table_field} = %s", $meta_field)); | |
// Remove repeater meta field | |
removeACFDBMeta($meta_field, false, $table, $table_field, $table_value); | |
if (!empty($max_repeater)) { | |
// Meta field repeater loop | |
for ($i = 0; $i < $max_repeater; $i++) { | |
$meta_field = $prefix ? $prefix . '_' . $field . '_' . $i : $field . '_' . $i; | |
removeACFDBMeta($value, $meta_field, $table, $table_field, $table_value); | |
} | |
} | |
} | |
} | |
} else { | |
// meta_key(prefix is repeater parent field with child fields) | |
$meta_field = $prefix ? $prefix . '_' . $meta : $meta; | |
// Specified Table Removal | |
$results = $wpdb->query("DELETE FROM {$wpdb->$table} WHERE {$table_field} REGEXP '{$meta_field}$'"); | |
// Options Table Removal(Remove if not needed) | |
$option_results = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name REGEXP '{$meta_field}$'"); | |
$results += $option_results ? $option_results : 0; | |
// Print results | |
print_r($meta_field . ' - ' . $results . ' rows removed in ' . $wpdb->$table . ' and ' . $wpdb->options); | |
echo '<br />'; | |
} | |
} | |
/** | |
* One Time Function Call, below is fields, and field repeater | |
* Note: Should only be called and used on local development, please remove from site when you deploy | |
*/ | |
removeACFDBMeta(array( | |
'dl_link_type', | |
'dl_section_title', | |
'dl_array' => array( | |
'dl_school_name', | |
'dl_school_url', | |
'dl_school_description' | |
), | |
'addl_content' | |
)); | |
/** | |
* One Time Function Call, below is a single field | |
* Note: Should only be called and used on local development, please remove from site when you deploy | |
*/ | |
removeACFDBMeta('dl_link_type'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment