Skip to content

Instantly share code, notes, and snippets.

@jpgninja
Last active January 3, 2019 19:41
Show Gist options
  • Save jpgninja/a234a8e9bb2c14671d609e8f6977187b to your computer and use it in GitHub Desktop.
Save jpgninja/a234a8e9bb2c14671d609e8f6977187b to your computer and use it in GitHub Desktop.
WordPress Snippets
## Delete duplicate WooCommerce products
## (Retains the lowest ID)
## Pro tip: Change to SELECT to test
DELETE bad_rows.*
from wp_posts as bad_rows
inner join (
select post_title, MIN(id) as min_id
from wp_posts
where post_type = 'product'
group by post_title
having count(*) > 1
) as good_rows
on good_rows.post_title = bad_rows.post_title
and good_rows.min_id <> bad_rows.id
## (Retains the highest ID)
## Pro tip: Change to SELECT to test
DELETE bad_rows.*
from wp_posts as bad_rows
inner join (
select post_title, MAX(id) as min_id
from wp_posts
where post_type = 'product'
group by post_title
having count(*) > 1
) as good_rows
on good_rows.post_title = bad_rows.post_title
and good_rows.min_id <> bad_rows.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment