Deleting posts directly in the DB is not going to clear up all their comments, post metadata, etc. I’d recommend using wp_delete_post()
to do this instead.
// Gets the post IDs.
$author_id = get_user_by( 'login', 'newsagencies' ); // Use the user's login here.
$posts = new WP_Query(
array(
'author' => $author_id,
'date_query' => array(
'after' => '2023-12-01',
'before' => '2024-03-31',
),
'fields' => 'ids',
'posts_per_page' => -1, // Get *all* the posts.
),
);
$force_delete = true; // Set this to 'false' if you only want to trash
// the posts, not actually delete them permanently.
if ( ! empty( $posts->posts ) ) {
foreach ( $posts->posts as $post_id ) {
wp_delete_post( $post_id, $force_delete );
}
}
Notes
- With 50,000 posts, this might take a while.
- This code is meant as a starting point only; it’s not production-ready, and should be tested on a test site first.