简体   繁体   中英

Wordpress SQL Delete Custom Post Types and Meta

I have the following SQL query which is able to fetch and delete all of the posts of a custom post type that is older than X days.

SELECT * FROM `wp_posts`
WHERE `post_type` = ‘clothing’
AND DATEDIFF(NOW(), `post_date`) > 2

DELETE * FROM `wp_posts`
WHERE `post_type` = ‘clothing’
AND DATEDIFF(NOW(), `post_date`) > 2

However from what I've read online, it seems that the code above doesn't really delete the posts's meta information, so I'll still have a bunch of left over data.

My question is, how can I modify this code so that all of the related meta information is also removed from the deleted posts?

Thanks

You can delete the data by joining the tables. In WP wp_posts and wp_postmeta are related with post_id in the wp_postmeta table. Using the query below it will delete from both tables. However there are other options as well ie you can fire a trigger after delete on wp_posts to delete the data from the related table or a foreign key constraint with on delete cascade

delete
p,pm
from wp_posts p
join wp_postmeta pm on pm.post_id = p.id
where p.post_type = 'clothing'
and DATEDIFF(NOW(), p.post_date) > 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM