Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

SELECT * 
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
add comment

2 Answers

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
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 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
add comment

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:

<?php
    define('WP_USE_THEMES', false);
    require('wp-blog-header.php');

    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
add comment

Your Answer

 
discard

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.