WordPress: Delete all posts from a given date with PHP function

Here is a small PHP function that deletes all posts from a given date. The function is inserted in your functions.php where you enter the date you want to delete posts from in date_query. You can quickly run the function via a GET url. Delete the function from your functions.php again when you're done using it, or make a more secure way to run the function than GET.

*/ Get posts */
function GetPostsLENNARTC() {
	$args = array( 'post_type' => 'post', 'posts_per_page' => -1,
  'date_query' => array(
'year' => 2022,
'month' => 11,
'day' => 05,
	$posts = get_posts( $args );
	foreach ( $posts as $post ) {
		$id = $post->ID;
			change_post_status($id, "trash"); // Post statuses: publish|pending|draft|private|static|object|attachment|inherit|future|trash
			var_dump('drafted: '. $id);

*/ Change post status function */
function change_post_status($post_id,$status){
	$current_post = get_post( $post_id, 'ARRAY_A');
	$current_post['post_status'] = $status;

*/ the running function via GET */

Not sure if you're deleting the right WordPress posts?

If you want to be sure that you don't delete any wrong posts and that the function runs correctly, you can change the post status to "draft" and then run this function in your database (PHPMyAdmin). This SQL function will delete all posts that have the status "draft".

DELETE a,b,c
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)
WHERE a.post_status = \'draft \'