I want to delete some unnecessary metatable from WordPress database, whenever I log out. I used the below code, but it does not work. I mean it's not deleting the table which I mentioned.
<?php
function delete_useless_post_meta()
{
global $wpdb;
$table = $wpdb->prefix.'postmeta';
$wpdb->delete ($table, array('meta_key' => '_edit_last'));
$wpdb->delete ($table, array('meta_key' => '_edit_lock'));
$wpdb->delete ($table, array('meta_key' => '_wp_old_slug'));
}
add_action('wp_logout','delete_useless_post_meta');
?>
It's best not to interact with the database directly, especially when issuing DELETE statements, since a single typo can destroy unintended data. Instead, use WordPress functions to get a list of all user IDs, then remove the user meta field for each user individually, like so:
$all_user_ids = get_users( 'fields=ID' );
foreach ( $all_user_ids as $user_id ) {
delete_user_meta( $user_id, 'your_meta_key_to_delete' );
}
Function reference: https://codex.wordpress.org/Function_Reference/get_users https://codex.wordpress.org/Function_Reference/delete_user_meta
OR
Other Option like, you can use the Adminimize plugin - Adminimize plugin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With