WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
There are over 700 records on the 80 or so posts on this site. – Mike Wills Oct 13 '10 at 18:34
up vote 6 down vote accepted

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

share|improve this answer
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 Oct 13 '10 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! – John P Bloch Oct 13 '10 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]);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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