Delete Unnecessary Post Metadata

By default every time you write or update any Post or Page in WordPress, it creates two very distinctive Post metadata. One of them is _edit_lock and another one is _edit_last. Not to mention they are created for a very specific reason. The "_edit_lock" metadata helps users to have control over their post during the time of editing. It is fairly useful when you have multiple users in one site. It prevents another user from taking over the control or editing capability from the original user. On the other hand "_edit_last" basically stores the time when it was last edited. So far so good.

The problem is, for every single post or page these two post metadata gets stored on your database permanently. It also means, if you have 100 post written on your site, most probably you would have 200 post meta entries that are pretty much useless yet occupying your database space. Over time, these data will pile up on your database and potentially slow down your database query. Another issue is, if you rename the post title of your old post, it will also create another row on your wp_postmeta (generally) table which is known as _wp_old_slug meta key.

Now all these post meta data is basically useless. However, there are ways to get rid of them. You can either delete them manually from the table or use a plugin to get your job done. Interestingly, I figured out a way to get rid of them with different approach. I wanted to have these meta data stored while I am working and then delete them as I log out from the system. Here is your magic function that you may have been looking for.

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');

All you need to do now, is to copy this snippet and paste it on your theme's functions.php page. Now you can do what you do best and stop worrying about your database space. This snippet actually could be your life saver if you are building a large database or if you have large number of entries.

Explanation

I created a simple function which has been hooked with add_action function and this function would only be called while user is signing out from the system. Within the function, I called for $wpdb object as I intended to delete certain row from the "wp_postmeta" table on your database. Unless you make any change to your table prefix during WordPress installation, your default post meta table should be "wp_postmeta". However, in my case it wasn't the default one. In order to avoid possible error, I created $table variable that will retrieve the prefix of your post meta table automatically and hold it on the memory. Then performs "deletion" process from the table that holds specific "meta_key". This is as simple as it can get.

Note: Please back up your database before you start using this snippet. I personally tested this function on my computer and on my web server and performed without any issue. I do not expect any hiccup in your case either. I hope you would find this post to be useful. Feel free to try out this snippet and let me know if you have any question regarding this issue.

Related

Comments

Comments list