• 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?

Viewing 1 replies (of 1 total)
  • Thread Starter mvtilborg

    (@mvtilborg)

    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");

Viewing 1 replies (of 1 total)
  • The topic ‘How to delete all posts with a given meta value’ is closed to new replies.