Skip to content Skip to sidebar Skip to footer

Wordpress Delete Posts Not In Specific Categories Using Wpdb Query

How do I modify the following query to delete all posts with the custom post type 'listings' that are NOT IN specific Wordpress categories? Note, I must use $wpdb->query() in my

Solution 1:

$wpdb->query('DELETE wp_posts FROM wp_posts 
INNER JOIN wp_term_relationships ON wp_posts.id = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy ON wp_term_relationships.object_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_posts.post_type = "listings" AND wp_term_taxonomy.taxonomy NOT IN (taxonomy_1_slug, taxonomy_2_slug, etc.)');

Solution 2:

Using category id delete post

   delete a,b,c,d
    FROM esx_posts a
    LEFT JOIN esx_term_relationships b ON ( a.ID = b.object_id )
    LEFT JOIN esx_postmeta c ON ( a.ID = c.post_id )
    LEFT JOIN esx_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
    LEFT JOIN esx_terms e ON ( e.term_id = d.term_id )
    WHERE e.term_id = 48

Post a Comment for "Wordpress Delete Posts Not In Specific Categories Using Wpdb Query"