سؤال

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?

هل كانت مفيدة؟

المحلول

//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...

نصائح أخرى

You can try to approach that from WP/PHP side, but I think in this case it would be easier to run replacement on database.

There are several tools around, for example Search and Replace for WordPress Databases. Just be sure to properly back up database before it and test on separate install if possible.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top