InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress Mysql Query tutorials

  • How to delete orphan wordpress wp_postmeta rows
  • Mysql query to find all yoast meta description
  • Query to find recently modified posts in Wordpress
  • Wordpress - an approach to bulk add tags to posts
  • Wordpress - query to dump all categories
  • Wordpress - query to dump all posts with featured image
  • Wordpress - query to dump all tags
  • Wordpress - query to find all posts for a category
  • Wordpress - query to find term_taxonomy_id from category name
  • Wordpress - query to find term_taxonomy_id from tag name
  • Wordpress mysql query to get all custom keys and values for a post
  • Wordpress mysql query to get all posts with a missing custom field
  • Wordpress mysql query to get all posts with a specific custom field
  • migrate multiple mysql databases using mysqldump on Linux
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress Mysql Queries

How to delete orphan wordpress wp_postmeta rows

By admin on Nov 24, 2015

WordPress keeps the post meta values in wp_postmeta table. When a post is deleted, its meta data may become orphan. It is harmless data but it may be good idea to clean for better database performance. Here is how the table wp_postmeta looks like:
wordpress-wp-postmeta-table-sample-data

Follow these steps to delete orphan values from table wp_postmeta. Note that the table name may have a prefix different from “wp_” depending upon your wordpress installation options.

  1. Take a backup of the table wp_postmeta using the following SQL.

    CREATE TABLE wp_postmeta_pre_clean_bak SELECT * FROM wp_postmeta;
    

    You may get a message like “2482 rows affected, taking 293 ms”. Ensure that backup table is created and has data.

  2. Run the following query to find orphan rows in table wp_postmeta.

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

    This will show us the rows which needs to be deleted. Note down the number of rows.

  3. Run the following query to delete orphan rows from table wp_postmeta.

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

    Note the number of rows deleted from the output message. It should be same as number of rows we got in above step.

Suggested posts:

  1. PHP check if key exists in array
  2. Curl – follow redirects
  3. HTML li tag
  4. Mysql – get total queries since beginning
  5. WordPress – query to find all posts for a category
  6. Chrome – view javascript errors
  7. jQuery – difference between html() and text()
  8. How to create Linux instance on Amazon AWS/EC2 Classic
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Mysql, Sql, Tutorials, Wordpress, Wordpress Mysql Queries
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress