Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Created March 27, 2020 15:10
Show Gist options
  • Save twentyfortysix/a3e74a5b8a453e61bc939ffef1d9fa06 to your computer and use it in GitHub Desktop.
Save twentyfortysix/a3e74a5b8a453e61bc939ffef1d9fa06 to your computer and use it in GitHub Desktop.
<?php
function replacer($post_type, $field, $find, $replace, $wet){
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status' => 'any'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$replacement = str_replace($find, $replace, $the_query->post->{$field});
$upsert = array(
'ID' => $the_query->post->ID,
$field => $replacement
);
echo $the_query->post->{$field} . ' // ' . $replacement . '<br>';
// Update the post into the database
if($wet == true){
wp_update_post( $upsert );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment