Skip to content

Instantly share code, notes, and snippets.

@strsar
Created October 7, 2021 01:32
Show Gist options
  • Save strsar/a01449c1fa8b2b341242115209ba42fc to your computer and use it in GitHub Desktop.
Save strsar/a01449c1fa8b2b341242115209ba42fc to your computer and use it in GitHub Desktop.
[WP] Batch update posts
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1
) );
foreach ( $posts as $post ) {
$content = $post->post_content;
if(substr( $content, 0, 4 ) === "<img"){
$content = preg_replace('/<img(.*)>/i','', $content, 1);
$post_data['ID'] = $post->ID;
$post_data['post_content'] = $content;
wp_update_post( $post_data );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment