Search and replace titles of posts with specific tags

//Search by tags
$query = new WP_Query( 'tag=old' );

// Then update post titles 
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        //check if title have "a new"
        if(strpos(get_the_title(),'a new') !== false) :

            $post = array(
                'ID'=> get_the_id(),
                'post_title'=> str_replace('a new','an old',get_the_title())
            );

            // Update the post into the database
            wp_update_post( $post );

        endif;
    }
}