businessdad icon

Aelia - Remove duplicate metadata created by Subscriptions 2

businessdad | PRO | 11/25/15 08:45:44 PM UTC | 0 ⭐ | 859 👁️ | Never ⏰ | []
PHP |

820 B

|

None

|

0 👍

/

0 👎

DELETE from wp_postmeta
WHERE 
  meta_id IN (
    SELECT meta_id_to_remove
    FROM (
      SELECT
        PM.post_id
        ,PM.meta_key
        /* - Use MIN() if the original meta was added a second time, and it has a higher ID
         *   than the duplicate.
         * - Use MAX() if the original meta was migrated (i.e. post ID changed),
         *   and it has a lower ID than the duplicate.
         */
        ,min(PM.meta_id) as meta_id_to_remove
        ,count(PM.meta_key) as meta_count
      FROM
        wp_posts P
        JOIN
        wp_postmeta PM ON
        (PM.post_id = P.ID) AND
        (PM.meta_key like '%_base_currency')
      WHERE
        (P.post_type = 'shop_order')
      GROUP BY
        PM.post_id
        ,PM.meta_key
      HAVING
        meta_count > 1
    ) AS duplicates
  )

Comments