Run `wp_insert_post_data` on all posts

Question

I have a bunch of regular expressions that I want to run on my posts to update the post_content field, when a series of conditions are met, some of them require a combination of various values from ACF. Long story short I need a way to be able to programmatically edit the content of all my posts using preg_replace

I’ve been learning about the wp_insert_post_data filter, and it works, but it only works for the current post that is being edited. Is there any way to do this in WordPress where I can loop through all posts and update the content similarly?

Here’s what I currently have.

add_filter('wp_insert_post_data', 'mystuff', 99, 2);
function mystuff($data, $post)
{
    $content = $data['post_content'];
    $regex = '/MYCRAZYREGEXSTUFF/';
    $match = preg_match($regex, $content, $matches);
    if ($match) {
        $updatedContent = preg_replace($regex, '', $content);
        $data['post_content'] = $updatedContent;
    }
    return $data;
}
0
Joe Seifi 8 months 2020-12-05T15:10:45-05:00 0 Answers 11 views 0

Leave an answer

Browse
Browse