Skip to content

Instantly share code, notes, and snippets.

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 kurtschlatzer/faf52ba90bc1af1c7137ecce9857c3a6 to your computer and use it in GitHub Desktop.
Save kurtschlatzer/faf52ba90bc1af1c7137ecce9857c3a6 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