• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP Bitz

A curated collection of code snippets for WordPress

  • Home
  • About
  • Blog
  • Code Snippets
You are here: Home / WordPress Development Tips / Check and delete orphaned post meta in WordPress

Check and delete orphaned post meta in WordPress

WordPress Development Tips · July 8, 2021

This post brought to you by RocketGeek, ButlerBlog, and the following:

 

Through the natural course of managing a WordPress site and its database, you may end up with some orphaned post meta. This is especially true if you run any delete queries on the posts table directly in MySQL, or if you do a lot of plugin/custom code development and/or testing.

The following MySQL queries can help you check for and delete any orphaned post meta entries (i.e. those post meta entries where no post ID matches).

IMPORTANT: be sure to backup your database before running any direct MySQL queries – especially those that delete data.

Get a list of orphaned post meta

SELECT * 
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

Delete orphaned post meta

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

Filed Under: WordPress Development Tips

Chad Butler

This post brought to you by RocketGeek, ButlerBlog, and the following:

 

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Join for free!

Get free code snippets, WordPress best practices, and practical Content Marketing advice from seasoned WordPress expert Chad Butler (butlerblog):

Recent Posts

  • Bitnami WordPress Autoptimize cannot write to the cache directory
  • Create and delete WordPress sites in XAMPP with a batch file
  • How to fix “Error: MySQL Shutdown Unexpectedly” in XAMPP control panel
  • Fix missing Customizer in WordPress 6
  • Use Proofy.io API to validate WordPress registrations

Copyright © 2023 · Maker Pro on Genesis Framework · WordPress · Log in