Halloween Sale 15% off Ends in See the plans
PHP

Orphaned Metadata Cleanup Tool

Adds Tools > Orphaned Metadata, which counts post, comment, term and user meta rows whose post, comment, term or user no longer exists, and removes them after you confirm.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'admin_menu', function () {
    add_management_page( 'Orphaned Metadata', 'Orphaned Metadata', 'manage_options', 'wpide-orphaned-meta', function () {
        global $wpdb;

        $tables = array(
            'Post meta'    => array( $wpdb->postmeta, 'post_id', $wpdb->posts, 'ID' ),
            'Comment meta' => array( $wpdb->commentmeta, 'comment_id', $wpdb->comments, 'comment_ID' ),
            'Term meta'    => array( $wpdb->termmeta, 'term_id', $wpdb->terms, 'term_id' ),
            'User meta'    => array( $wpdb->usermeta, 'user_id', $wpdb->users, 'ID' ),
        );

        if ( isset( $_POST['wpide_clean_orphans'] ) && check_admin_referer( 'wpide_clean_orphans' ) && current_user_can( 'manage_options' ) ) {
            $deleted = 0;
            foreach ( $tables as list( $meta, $fk, $parent, $pk ) ) {
                $deleted += (int) $wpdb->query( "DELETE m FROM $meta m LEFT JOIN $parent p ON p.$pk = m.$fk WHERE p.$pk IS NULL" );
            }
            wp_cache_flush();
            printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( sprintf( 'Removed %d orphaned row(s).', $deleted ) ) );
        }

        echo '<div class="wrap"><h1>Orphaned Metadata</h1><table class="widefat striped" style="max-width:520px"><thead><tr><th>Type</th><th>Orphaned rows</th></tr></thead><tbody>';
        $any = 0;
        foreach ( $tables as $label => list( $meta, $fk, $parent, $pk ) ) {
            $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM $meta m LEFT JOIN $parent p ON p.$pk = m.$fk WHERE p.$pk IS NULL" );
            $any  += $count;
            printf( '<tr><td>%s</td><td>%s</td></tr>', esc_html( $label ), esc_html( number_format_i18n( $count ) ) );
        }
        echo '</tbody></table><form method="post" onsubmit="return confirm(\'Delete all orphaned metadata rows?\');">';
        wp_nonce_field( 'wpide_clean_orphans' );
        submit_button( 'Delete orphaned rows', 'primary', 'wpide_clean_orphans', true, $any ? array() : array( 'disabled' => 'disabled' ) );
        echo '</form></div>';
    } );
} );

Notes

Leftover rows usually come from deleted plugins or direct database edits; take a backup before deleting.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.