Delete/Replace words from X to Y in post content
Untested, but I believe you can do something like this:
add_action( 'init', function() {
// since were editing content, we want to be very very careful
// well do a dry run output first to make sure were doing what we want
$dryrun = true;
// Regex pattern finds anything between the X and Y bounds.
$pattern = '/(XXXX)(.+?)(YYYY)/gms';
// Get and cycle through ALL published `posts`
$all_posts = get_posts('posts_per_page=-1');
foreach ($all_posts as $a_post) {
// replace the pattern with our new content.
// If the content is to be preserved, we could use $2 in the second arg
$new_content = preg_replace($pattern, "New content here", $a_post->post_content);
// if somthing was replaced, lets continue
if ( $new_content != $a_post->post_content ) {
// display what _would_ be changing
if ( $dryrun ) {
echo "<div>update post #{$a_post->ID} to:</div>";
echo "<pre>{$new_content}</pre>";
} else {
// update the post_content with our new content
wp_update_post(array(
'ID' => $a_post->ID,
'post_content' => $new_content,
));
}
} else {
// nothing was changed, if dry run, just print the notice.
if ( $dryrun ) {
echo "<div>nothing to update in post #{$a_post->ID}</div>";
}
}
}
});
Even though this code has a dry run on it, always back up your database content and verify it before doing mass-modifying like this, things can go horribly wrong very quickly.