Moozy
Moozy

Reputation: 5015

Woocommerce delete products which are "Out of stock"

So, as the title, my question is how I can:
Delete all products which are "Out of stock", not only to hide them, but to delete from the DB.
I found the following code but it is deleting all products, not only those that are out of stock:

DELETE p FROM wp_posts p WHERE p.post_type = 'product';

DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;

DELETE tr FROM wp_term_relationships tr INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.taxonomy != 'link_category' AND tr.object_id NOT IN ( SELECT ID FROM wp_posts );

Upvotes: 0

Views: 5877

Answers (2)

Rohil_PHPBeginner
Rohil_PHPBeginner

Reputation: 6080

Try this :

DELETE p FROM wp_posts p WHERE p.post_type = 'product' and p.id = '$post->ID';

EDITED :

DELETE p FROM wp_posts p join wp_postmeta m on p.ID = m.post_id WHERE p.post_type = 'product' and m.meta_key='_stock' and m.meta_value='0';

Upvotes: 1

Anand Shah
Anand Shah

Reputation: 14913

Products is a custom post type, you can delete "out of stock" products by selecting such products using meta query from within WordPress

However if you want to go the SQL Query way, the following query will do the job

DELETE p FROM wp_posts p join wp_postmeta pm on p.ID = pm.post_id WHERE p.post_type = 'product' and pm.meta_key='_stock_status' and pm.meta_value='outofstock';

Upvotes: 4

Related Questions