Advertisement
businessdad

Aelia - Remove duplicate metadata created by Subscriptions 2

Nov 25th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. DELETE from wp_postmeta
  2. WHERE
  3.   meta_id IN (
  4.     SELECT meta_id_to_remove
  5.     FROM (
  6.       SELECT
  7.         PM.post_id
  8.         ,PM.meta_key
  9.         /* - Use MIN() if the original meta was added a second time, and it has a higher ID
  10.          *   than the duplicate.
  11.          * - Use MAX() if the original meta was migrated (i.e. post ID changed),
  12.          *   and it has a lower ID than the duplicate.
  13.          */
  14.         ,min(PM.meta_id) as meta_id_to_remove
  15.         ,count(PM.meta_key) as meta_count
  16.       FROM
  17.         wp_posts P
  18.         JOIN
  19.         wp_postmeta PM ON
  20.         (PM.post_id = P.ID) AND
  21.         (PM.meta_key like '%_base_currency')
  22.       WHERE
  23.         (P.post_type = 'shop_order')
  24.       GROUP BY
  25.         PM.post_id
  26.         ,PM.meta_key
  27.       HAVING
  28.         meta_count > 1
  29.     ) AS duplicates
  30.   )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement