How to apply content filter permanently?

I have the following temporary filter to fix a content error. We have data in our posts that have 4 digit zip codes, because the leading zero was cut when the original file was created. (For instance, a Massachusetts zip code is 0XXXX, but now in our content it is XXXX)

function fix_zip_code($content){
    if (preg_match('/<span class="postal-code">(d{4})</span>/i',$content, $match)){
        $content = str_replace($match[0],'0'.$match[0],$content);
    }
    return $content;
}
add_filter('the_content', 'fix_zip_code');

How can I apply that filter to all the content at once and re-save the posts so I can remove the regex filter completely?

Related posts

Leave a Reply

2 comments

  1. //run once
    
    $allposts = get_posts('post_status=publish&numberposts=-1');
    foreach ($allposts as $thispost) {
         wp_update_post( array(
              'ID' => $thispost->ID,
              'post_content' => fix_zip_code($thispost->post_content)
              )
         );
    }
    

    But as Rarst mentioned, backup your database before doing anything…