Skip to content

Instantly share code, notes, and snippets.

@cyberhobo
Created October 4, 2012 19:31
Show Gist options
  • Save cyberhobo/3835864 to your computer and use it in GitHub Desktop.
Save cyberhobo/3835864 to your computer and use it in GitHub Desktop.
Sometimes post imports in WordPress can create duplicate custom fields for posts that already exist. This will delete custom fields on posts the have the same key and value. A post type restriction is included as an example of further narrowing.
DELETE o
/* SELECT p.post_title, o.*, pmi.* */
FROM wp_postmeta AS o
INNER JOIN wp_posts AS p ON p.ID = o.post_id
INNER JOIN wp_postmeta AS pmi ON pmi.post_id = o.post_id AND pmi.meta_key = o.meta_key AND pmi.meta_value = o.meta_value
WHERE p.post_type = 'post'
AND pmi.meta_id < o.meta_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment