Wordpress - How to delete custom taxonomy terms in plugin's uninstall.php?
Using the uninstall hook would be legitimate if it worked, but it dumps the same error as the uninstall file. It would have more chances of working as it loads the main plugin file to perform the uninstall function, but if the taxonomy is not registered anymore (and it is not, as we need to deactivate before uninstalling), WP cannot use get_terms
.
Adapting the following function from this Stack Overflow answer should do the work:
function load_terms($taxonomy){
global $wpdb;
$query = 'SELECT DISTINCT
t.name
FROM
`wp-cls`.wp_terms t
INNER JOIN
`wp-cls`.wp_term_taxonomy tax
ON
`tax`.term_id = `t`.term_id
WHERE
( `tax`.taxonomy = \'' . $taxonomy . '\')';
$result = $wpdb->get_results($query , ARRAY_A);
return $result;
}
Based on input from brasofilo, here is what I ended up doing:
function delete_custom_terms($taxonomy){
global $wpdb;
$query = 'SELECT t.name, t.term_id
FROM ' . $wpdb->terms . ' AS t
INNER JOIN ' . $wpdb->term_taxonomy . ' AS tt
ON t.term_id = tt.term_id
WHERE tt.taxonomy = "' . $taxonomy . '"';
$terms = $wpdb->get_results($query);
foreach ($terms as $term) {
wp_delete_term( $term->term_id, $taxonomy );
}
}
// Delete all custom terms for this taxonomy
delete_custom_terms(LISTING_TAXONOMY);