Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

cleaning up safely wordpress wp_postmeta table

The only generic semi-automatic procedure I can think of would be to delete post meta where the post_id is invalid.

SELECT * 
FROM {$wpdb->postmeta} as pm 
LEFT JOIN {$wpdb->posts} as p
  ON pm.post_id = p.ID
WHERE p.ID IS NULL

I wrote a SELECT and not a DELETE because I don’t trust that to work in all cases. I have seen “dummy” or “placeholder” values in the postmeta table with a post_id of 0 or some absurdly high value. I am not saying those are good ideas, but I have seen that, so be aware. You are better off looking over the data returned by that SELECT and then deleting items selectively.

I don’t really expect to get very far a post meta makes very little sense without post, except for a few questionably wise uses.

The other, very labor intensive thing you can do is look at the meta_keys in the database and try to spot keys belonging to plugins that you no longer use. It may or may not be possible to identify them, but you can try. Some plugins use identifiable prefixes or suffixes.

Related Posts:

  1. Most efficient way to get posts with postmeta
  2. Explanation of update_post_(meta/term)_cache
  3. Query between two meta values?
  4. Get post from meta_key and meta_value
  5. WP_POSTMETA changes site crash
  6. How to stop wp_postmeta from being called on archive and search pages?
  7. What type of index should I use for postmeta?
  8. Custom query, multiple custom keys
  9. How do I insert a new meta key / value pair, but only if another meta key is present?
  10. mass delete posts based on metadata
  11. Duplicate rows in meta table, any known relations in WC?
  12. Need a SQL query to update meta_key=’_price’ with value in meta_key=’_regular_price’
  13. SQL query – get a featured image’s alt / alternative text
  14. Removing Malware
  15. Optimize WP Meta Query for large amount of post meta?
  16. Which query method to use? (edit- wpdb syntax problems)
  17. Cache metadata for set of posts
  18. What’s the best way to search for a UPC code in a Database?
  19. SQL: How to properly check if a record exists
  20. How do I retrieve the slug of the current page?
  21. Get posts by meta value
  22. what is the correct way to compare dates in a WP query_posts meta_query
  23. How to extract data from a post meta serialized array?
  24. What SQL Query to do a simple find and replace
  25. Attaching taxonomy data to post with wp_insert_post
  26. How to save an array with one metakey in postmeta?
  27. WordPress is stripping escape backslashes from JSON strings in post_meta
  28. How to benchmark a WordPress installation? [closed]
  29. How can I get the post ID from a WP_Query loop?
  30. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  31. Correctly delete posts with meta and attachments [duplicate]
  32. How to update_post_meta value as array
  33. Adding meta tag without plugin
  34. What’s the point of get_post_meta’s $single param?
  35. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  36. How to edit a post meta data in a Gutenberg Block?
  37. Sanitizing integer input for update_post_meta
  38. post formats – how to switch meta boxes when changing format?
  39. Execute action after post is saved with all related post_meta records (data)
  40. Scaling a WP eCommerce site
  41. Lack of composite indexes for meta tables
  42. Get a single post by a unique meta value
  43. if get_post_meta is empty do something
  44. How we get_post_meta without post id
  45. How get post id from meta value
  46. What is the code to get the download link for a product in WooCommerce?
  47. Safe to delete blank postmeta?
  48. Dealing with Many Meta Values, 30+
  49. advanced custom fields update_field for field type: Taxonomy
  50. update_post_meta not saving when value is zero
  51. Export WordPress Posts and Meta Information in CSV format
  52. Content hooks vs User hooks
  53. Meta compare with date (stored as string) not working
  54. Trying to get custom post meta through Jetpack JSON API [closed]
  55. How to update/insert custom field(post meta) data with wordpress REST API?
  56. Restrict post edit/delete based on user ID and custom field
  57. get_post_meta returning empty string when data shows in the database
  58. publish_post action hook doesn’t give post_meta_data
  59. Remove WordPress.org Meta link
  60. Remove post meta keys
  61. How to access the post meta of a post that has just been published?
  62. Why time functions show invalid time zone when using ‘c’ time format?
  63. Why is get_post_meta returning an array when I specify it as single?
  64. How to delete outdated, wrongly sized images in _wp_attachment_metadata?
  65. How to update/delete array in post meta value?
  66. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  67. Adding an assisting editor box to Post page
  68. Meta keywords and descriptions plugin for manually editing meta for each page/post
  69. How to display multiple Post meta_key/meta_values by SQL query
  70. Manipulating post meta in the customizer
  71. How to remove in the wordpress database all posts revisions except the last three?
  72. delete unused postmeta
  73. Should I sanitize custom post meta if it is going to be escaped later?
  74. Add post meta based on another post meta value before publish post
  75. What is an efficient way to query based on post_meta?
  76. How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  77. Front-end update_post_meta snippet displays white screen?
  78. Save both current and new version of post meta
  79. Get Advanced Custom Fields values before saving [closed]
  80. Give extra post-meta to RSS feeds
  81. How to get meta value in wp_attachment_metadata
  82. WP REST API “rest_no_route” when trying to update meta
  83. Clean up output added via wp_head()
  84. List posts under meta_value heading
  85. Why am I getting an infinite loop with have_posts?
  86. Up/Down voting system for WordPress
  87. get_post_meta – get a single value
  88. Custom Fields Bulk Edit
  89. delete value 0 in post meta [closed]
  90. How to solve slow WordPress site caused by attachment_metadata
  91. Can I safely delete a record, manually, in the wp postmeta table?
  92. How to store post meta in an array?
  93. What action hook updates post meta?
  94. Can’t translate the post meta data (Date) in another language
  95. get_post_meta / update_post_meta array
  96. Updating post meta for custom post types
  97. adding a URL to a post meta
  98. Having trouble with custom post type / meta box
  99. Exclude a category from the filed under list
  100. ajax delete value from custom field array
Categories post-meta Tags customization, optimization, post-meta, sql
how to show only specific category for a template
Use another menu when logged in

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress