mvtilborg
Member
Posted 6 months ago #
I want to delete all posts that have a specific meta_value for a given meta_key, how can I best do this?
And also when deleted the posts, do then the meta_key and values gohst around? in other words do I need to delete those seperately from deleting the posts itself?
mvtilborg
Member
Posted 6 months ago #
Got this now, but does not do anything, what is wrong? Any help is highly appreciated.
global $wpdb;
// delete all posts WHERE meta_value = $aid;
$wpdb->query("DELETE wp
FROM $wpdb->posts wp
LEFT JOIN $wpdb->postmeta pm ON pm.post_id = wp.ID
WHERE mp.meta_key = 'product_aid' AND mp.meta_value = '". $aid ."'");
// delete not used meta
$wpdb->query("DELETE pm
FROM $wpdb->postmeta pm
LEFT JOIN $wpdb->posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL");