Skip to content

Instantly share code, notes, and snippets.

@dlh01
Last active February 1, 2023 03:36
Show Gist options
  • Save dlh01/32e7e46f98e368711e8b62b791725e2c to your computer and use it in GitHub Desktop.
Save dlh01/32e7e46f98e368711e8b62b791725e2c to your computer and use it in GitHub Desktop.
WP Bulk Task example
<?php
( new \Alley\WP_Bulk_Task\Bulk_Task( 'bananaify' ) )->run(
[
'post_status' => 'publish',
],
function ( $post ) {
if ( str_contains( $post->post_content, 'apple' ) ) {
$new_value = str_replace( 'apple', 'banana', $post->post_content );
if ( ! empty( $assoc_args['dry-run'] ) ) {
\WP_CLI::log( 'Old post_content: ' . $post->post_content );
\WP_CLI::log( 'New post_content: ' . $new_value );
} else {
$post->post_content = $new_value;
wp_update_post( $post );
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment