You may use a simple query and loop with wp_update_post function to update the post providing ID, post type by checking the words with preg_match
The following example shows a simple update to the post status for all posts in a loop for which post title does not contain Hello
or content does not contain Hello
.
The following code is proved to work by just putting in theme functions.php .
$args = array(
'post_type' => 'post', // any post type
'post_status' => array( 'publish', 'private', 'draft' ), // any types
);
$query_post = new WP_Query( $args );
if ( $query_post->have_posts() ) {
while ( $query_post->have_posts() ) {
$query_post->the_post();
$postarr = array(
'ID' => get_the_ID(),
'post_type' => 'post',
'post_status' => 'publish', // change to other type needed such as draft
);
// eg. if not contain specific words in title or content
if( ! preg_match( "/Hello/", get_the_title() ) || ! preg_match( "/Hello/", get_the_content() ) ) {
$result = wp_update_post( $postarr );
// for debug
var_dump($result); // (int|WP_Error) The post ID on success. The value 0 or WP_Error on failure.
}
}
wp_reset_query();
}
For some cases, depends on the loading of plugin and theme settings, putting inside a hook such as init hook
will ensure it run with all necessary components is loaded
// in functions.php
add_action( 'init', 'ws365194_my_test' );
function ws365194_my_test() {
// paste above code here and run
}