Skip to content

Instantly share code, notes, and snippets.

@alexpirine
Last active October 2, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpirine/2296581 to your computer and use it in GitHub Desktop.
Save alexpirine/2296581 to your computer and use it in GitHub Desktop.
Deletes postmeta duplicates
<?php
# Copyright Alexandre Syenchuk, 2012
# a multi-site, multi-duplicates version of https://gist.github.com/1177018
header('Content-type: text/plain; charset=utf-8');
include('wp-config.php');
include('wp-load.php');
global $wpdb;
$req_tpl = <<<eof
DELETE FROM %s
WHERE meta_key = '_mf_write_panel_id'
AND post_id IN (SELECT * FROM (
SELECT post_id
FROM %s
WHERE meta_key = '_mf_write_panel_id'
GROUP BY post_id
HAVING COUNT(post_id) > 1
) AS tmp1)
AND meta_id NOT IN (SELECT * FROM (
SELECT meta_id
FROM %s
WHERE meta_key = '_mf_write_panel_id'
GROUP BY post_id
HAVING COUNT(post_id) > 1
) AS tmp2)
;
eof;
$blogs = $wpdb->get_results("SELECT blog_id FROM $wpdb->blogs");
foreach ($blogs as $blog)
{
$blog_id = $blog->blog_id;
$table = $wpdb->get_blog_prefix($blog->blog_id).'postmeta';
$req = str_replace('%s',$table,$req_tpl);
$changes = $wpdb->query($req);
echo "$blog_id : ".($changes ? "$changes modifications" : "ok")."\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment