I am in the process of cleaning up a site after migrating it from a single install of WP to a multi-site install of WP. I have noticed many, many duplicate custom fields for each post. I assume this is from older versions of plugins that didn't check for the field before adding another. In the migration I am dropping these plugins.

Could I just delete these fields in the back-end using SQL or is there a better way?

FROM  `wp_5_postmeta` 
WHERE  `meta_key` IN ("podPressPostSpecific", "aktt_tweeted", "podPressMedia")

Only using DELETE obviously.

  • There are over 700 records on the 80 or so posts on this site.
    – Mike Wills
    Commented Oct 13, 2010 at 18:34

2 Answers 2


If you're not going to use those plugins, have at it. No reason to keep them around. Direct SQL query should be fine.

Related: add_post_meta() has a nifty argument to prevent this very problem. The fourth argument is a boolean, declaring whether the meta should be singular:

add_post_meta( $post->ID, 'my_foo_bar', 'value', true );

Source: Add Post Meta | WP Codex

  • 1
    Not sure what is the point of last example. How is it different from doing update_ alone? As far as I understand form docs update_ either creates value (if not present) or edits (if present), it can't create duplicate anyway?..
    – Rarst
    Commented Oct 13, 2010 at 19:10
  • You know what, you're absolutely right. I never looked at the source code before and had just gone off of what the codex had said. Thanks for pointing that out! Commented Oct 13, 2010 at 19:35

If you want to delete all the fields, do it directly in the database, or in admin area. But if you'd like to keep one copy of each field, and only delete the duplicates, well, it's a bit more complicated. You can delete directly from the database, using an intricate SQL statement like:

delete from wp_postmeta
where meta_id in (
       select *
       from (
               select meta_id
               from wp_postmeta a
               where a.meta_key = 'podPressPostSpecific'
               and meta_id not in (
                       select min(meta_id)
                       from wp_postmeta b
                       where b.post_id = a.post_id
                       and b.meta_key = 'podPressPostSpecific'
       ) as x

Don't forget to change name of the meta_key on both places if you want to delete duplicates for another custom field.

or you can use a php script for this. Example:

    define('WP_USE_THEMES', false);

    define( 'WP_DEBUG_DISPLAY', true ); 
    ini_set( 'display_errors', true );
    $allposts = get_posts('numberposts=-1&post_type=post&post_status=any');
    $keys = array('podPressPostSpecific', 'aktt_tweeted', 'podPressMedia');
    foreach ( $keys as $key ) {
        foreach( $allposts as $postinfo) {
            // Fetch array of custom field values
            $postmeta = get_post_meta($postinfo->ID, $key);

            if (!empty($postmeta) ) {
                // Delete the custom field for this post (all occurrences)
                delete_post_meta($postinfo->ID, $key);

                // Insert one and only one custom field
                update_post_meta($postinfo->ID, $key, $postmeta[0]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.