The s
parameter of WP Query search posts within the title and content columns of the posts table. You can use that to shorten your code. Here is a sample bit to help you get started.
You can hook it in wp_footer
to run only once (very bad idea to keep it there, but good for testing), or create a cron job to run it automatically on a selected interval.
function wpse388545_delete_selected_posts() {
$get_posts = get_posts(array(
'post_type' => array('post'),
'posts_per_page' => -1,
's' => 'This is the search term',
));
if ( !empty($get_posts) ) {
foreach( $get_posts as $post ) {
wp_delete_post($post->ID, true);
}
}
}