Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete unnecessary metatable from WordPress database?

Tags:

php

wordpress

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');
?>
like image 279
D.JCode Avatar asked Feb 14 '26 00:02

D.JCode


1 Answers

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

like image 126
Priyanka Modi Avatar answered Feb 16 '26 12:02

Priyanka Modi