Forked from jpgninja/delete-duplicate-products-woocommerce.sql
Created
January 3, 2019 19:41
-
-
Save kurtschlatzer/faf52ba90bc1af1c7137ecce9857c3a6 to your computer and use it in GitHub Desktop.
WordPress Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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