I have a sql code for wordpress database that use for deleting posts from specific cat id for specific time duration and I always use this code and it is working fine.
DELETE
a, -- DELETE from posts
b, -- DELETE from term relationships
c -- DELETE from postmeta
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = 2 AND DATEDIFF(NOW(), post_date) > 180;
but the issue is I want to keep those posts from the specific cat id that have another cat id checked. I know my description is confusing you so I make an example : Imagine I want to delete posts from cat id [2] for time more than 180 days but some of these cat id [2] posts are in another cat id for example [1] and I don't want delete these posts. Just delete posts that belong only to cat id [2]. the posts that are in cat id 2 and 1 should not delete. So in the above code I need to add additional code to can keep those posts that are in cat id [1] anyway. Right now the above code delete all posts in cat id [2] and also the posts in cat id [2] and [1] Let me explain this in another way:
I need a correct code that any posts from cat id [1] never and should not be deleted. There are many posts that are in cat id [1] and another cat id that should remain and never delete with this code.
wp your-command --category_id=2
. Even without WP-CLI, you could still create a PHP function/script and run it when necessary. But if you insist on using direct SQL, then be prepared to face issues like on thewp-admin
page for managing categories, the post count are incorrect (until for example after you created a new post).