My uninstall.php file won't delete my custom posts and the meta data related to them on uninstall. The plugin files get deleted, but nothing from the database.
If anyone is able to have a look at this and see if I've got something wrong or I'm missing something, I'd greatly appreciate it.
Thanks!
// If uninstall is not called from WordPress, or the current user doesn't have the capability to delete plugins
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) || !current_user_can_for_blog( $GLOBALS['blog_id'], 'delete_plugins' ) ) {
exit();
}
// Remove posts of the 'mrlpt_client' type from DB and the post meta
function mrlpt_delete_plugin() {
$mrlpt_client_posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'mrlpt_client',
'post_status' => 'any' ) );
foreach ( $mrlpt_client_posts as $mrlpt_client_post ) {
delete_post_meta( $mrlpt_client_post->ID, '_mrlpt_client_email' );
delete_post_meta( $mrlpt_client_post->ID, '_mrlpt_client_phone_num' );
wp_delete_post( $mrlpt_client_post->ID, true );
}
}
mrlpt_delete_plugin();
Edit:
I have tried using sql queries instead, and again it's not working quite right. I'm working in a multisite set-up, and the posts and post meta data ARE deleted on the MAIN site (wp_). However, the second site (wp_4_) is not working, nothing is deleted.
What am I missing to get this working in multisite? It's very odd because I have another sql query in my main plugin file to dynamically populate a drop-down field in a form and it works fine.
global $wpdb; // Must have this or else!
$postmeta_table = $wpdb->postmeta;
$posts_table = $wpdb->posts;
$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_email'");
$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_phone_num'");
$wpdb->query("DELETE FROM " . $posts_table . " WHERE post_type = 'mrlpt_client'");
Edit 2:
Hard-coding in the prefix of the second site (wp_4_) instead of using $wpdb, it then DOES delete the posts and meta data in the database. But, obviously I can't be hard-coding that in.
Any ideas as to what's going on here? I'm truly stumped.