How Do I Filter by Post ID Range and Category Using WP CLI?
-
I have just exported a bunch of posts to a new site and need to delete them from the old one. In the past I’ve used WP CLI to do this but this new case has a new challenge. I need to delete a range of posts less than or equal to a specific ID number for which the category ID equals a specific number. So, basically delete each post with ID<=##### AND category = ##.
I have used wp db query in the past but don’t know to do it this time because the posts table has no column for the category ID. A better database design would only store actual posts in the posts table and have a column for category ID because every post should have at least one category, but instead someone at WordPress decided to use the posts table to store all sorts of things most of which are not posts.
If I didn’t need to filter based on category ID, I would use the following for the deletion:
wp post delete $(wp db query 'SELECT ID FROM
wp_posts
WHEREID
<"89830" ANDpost_type
="post" ANDpost_status
="publish";' --skip-column-names)If I just needed to delete the category I would use this:
wp post delete $(wp post list --cat=25 --format=ids) --skip-plugins --skip-theme
How do I alter either of those to include the other? Preferable the second one should be alterable so that someone can type something like –id<=89830 unfortunately that is not an option. This means the first method will likely need to include a join with whatever table contains the record matching the post to a category which I believe to be the taxonomy table but I’m not sure exactly.
Could someone at WordPress please make up for this obvious lack of functionality which should’ve been in place years ago by posting SQL in response to this question which can be plugged into wp db query and achieve the desired result?
- The topic ‘How Do I Filter by Post ID Range and Category Using WP CLI?’ is closed to new replies.