WordPress SQL Query: Select And Delete Posts Between Two Dates
If you are wondering how to delete posts for a certain date, you will need to know how WordPress stores posts in its database.
In order to delete posts for a certain date we need to have a date, but that alone is not sufficient. WordPress stores the date like this:
As you can see it includes a timestamp. This goes for post_date and post_date_gmt.
So when you write an SQL query, you have to specify the time like 00:00:00
Select Rows For Regular Posts
Before we actually delete any entries, it’s a good practice to run a SELECT query to verify what it is that you’re getting rid of.
SELECT * FROM wp_posts WHERE post_date > '2015-06-25 00:00:00' AND post_date < '2015-07-10 00:00:00';
Only Select Rows For Custom Post Types
If you have set up your own post type such as “reviews”, “threads”, “products”, we need to further specify the post_type
SELECT * FROM wp_posts WHERE post_type = 'reviews' AND
post_date > '2015-06-25 00:00:00' AND post_date < '2015-07-10 00:00:00';
To actually delete the rows, you now only need to drop the statement “SELECT *” and replace it with “DELETE”. If you are entering the query via PHPMyadmin it should then ask you before it runs the query.