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

Delete all post meta except featured image Using SQL

I rewrote the function from the link in your question to SQL.
Keep in mind that this script will also clear the page-templates assigned to the pages.

Make a backup of the wp_postmeta table first.

DELETE pm 
FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.id
WHERE
    (p.post_type IN ('post', 'page') AND pm.meta_key <> '_thumbnail_id')
    OR (p.post_type="attachment" AND pm.meta_key <> '_wp_attached_file' 
        AND pm.meta_key <> '_wp_attachment_metadata')

Related Posts:

  1. Delete all post meta except featured image
  2. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  3. How to solve slow WordPress site caused by attachment_metadata
  4. Set Display Name to first and last name (phpmyadmin SQL Query)
  5. Size article thumbnails and retrieve them directly from database
  6. Emojis getting converted to “?”
  7. WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
  8. WordPress database import: Row size too large
  9. Changing root password in PHPMyAdmin for WordPress Database when going live
  10. Can’t update WordFence Options, clear data manually
  11. Files on Localhost, Database on Server
  12. Installing local to live WP into subdirectory
  13. DB prefix not updating
  14. I wanted to Know that how can I give ingore to 0000-00-00 in my config file
  15. I imported an restore an database and It shows #1067 – Invalid default value for ‘user_registered’
  16. “BS_” rows in postmeta table
  17. Migrating meta_key and meta_value from old theme to new one
  18. mySQL database size large, but tables size less
  19. Missing latest posts and options after DB migration via phpMyAdmin
  20. Check if a database is well installed
  21. How do i get value from wp_postmeta?
  22. featured image not showing up with mysql_connect
  23. SQL query : Select a thumbnail for a media
  24. #1067 – Invalid default value for ‘post_date’ when trying to create a new column
  25. Deleting WordPress posts and media files based on author via MYSQL database
  26. Recovery – Restore Database after moving folder location locally
  27. Replacing javascript link in WordPress database without getting mysql syntax errors
  28. Set a cron job to delete a specific database table table every midnight
  29. How to edit custom table data in frontend
  30. local wordpress broke after changing URL
  31. I need to find posts that contain YouTube links without http:// or https:// prefix and add http:// to all of them
  32. how to access 2nd Mysqli Database from different server for wordpress
  33. Is there any defects for using same database prefix in WordPress for multiple sites?
  34. Deleted products from woocommerce are still in the database
  35. Error establishing a database connection – WordPress setup, everything else is working
  36. Store GravityForm data in phpmyadmin (mysql)
  37. How can I find user role in Mysql?
  38. How to import products in host instead of database on wordpress?
  39. What is recommended data type and encryption type for password in a database
  40. syntax error for changing user role from database
  41. I would like some help wth an SQL query to link posts with categories
  42. Where is the HTML-handler part in the wpdb class?
  43. Exporting Post ID, Post Title, Primary Category and Primary Category ID
  44. Featured image from custom to WordPress database
  45. WordPress Database Query works in phpMyAdmin but not in the code
  46. Duplicate database to roll back theme & content changes
  47. How to map data on CSV to web pages?
  48. WP website showing blank page after moving from subdomain to main domain
  49. How to create index (sql) to a meta_key?
  50. next comment id number wordpress
  51. Database structure for thousands of posts
  52. Search and Replace in Windows XAMPP site
  53. Delete all users meta that named: user_avatar
  54. WPCLI search and replace in a particlar site dir effect another site-dir
  55. How can i syn wordpress user table with a stand-alone php script?
  56. insert data from a form:: Warning Empty query mysqli::query()
  57. How can I get $wpdb to show MySQL warnings?
  58. Merging development site with live site
  59. The MySQL Connection could not be established., before it was joomla but i have change it into worpress
  60. How to retrieve user data based on role using SQL?
  61. spambot registering without providing email or password, bypassing registration process
  62. After renaming MySQL database, I can no longer access my Blog page
  63. How To connect to the same WordPress database with different database user
  64. Possible to restore wordpress database from mysql56 and sqlite files?
  65. WordPress Database Posts Table query
  66. How much post meta data is too much?
  67. Hang Up Followed By Can’t select database
  68. WordPress character set issue
  69. How to extract some part of WordPress full source code
  70. Mysql Queries per Visit – Crazy High
  71. Store the wordpress “featured image” under »wp_posts« Database table
  72. Create a new table on the database and save parameters from a contact form
  73. Changed meta_key value in structures, now dashboard shows zero users
  74. live site do not update after importing sql database
  75. Why is converting my database to UTF-8 truncating entries?
  76. Widgets with *internal* links do not display after database migration
  77. using same mysql user with many databases
  78. Options table – where does my values go?
  79. Copy wp_postmeta from one database to another
  80. “MySQL server has gone away” since update to 3.8
  81. Why are my WordPress post queries so slow?
  82. Database question while Migrating from one domain to another on same WebHost [Bluehost]
  83. MySQL Database User: Which Privileges are needed?
  84. Import Excel (or csv) Table with user-data as subscribers to wordpress
  85. Print output of Table Creation
  86. Recommended Post Structure for DB Storage
  87. How to back-up a database on IIS
  88. Migrated to Namecheap, now Trouble with Database & wp-config [closed]
  89. Export posts with postmeta without ID?
  90. export individual posts to text files or a single csv file
  91. Importing Concrete5 content to WordPress
  92. Custom wordpress SQL statement for a website
  93. why you don’t Implement PDO for access every thing to database
  94. SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
  95. WordPress database connection failed, while mysql client is working [closed]
  96. How to find fresh (last modified) and sql backup?
  97. Uploading to WordPress Database
  98. Have WordPress Use a Different Database for users
  99. How to generate PDF dynamically based on values from the database in WordPress
  100. Understanding Redundant postmeta Tables in WordPress Database
Categories Database Tags database, mysql, phpmyadmin, post-meta, post-thumbnails
Checking if a post with certain meta value exists
WP_Query no posts with tax_query

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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