Automatically change the page password for more than one page

To make this work on multiple posts, your options are either

  1. write the ‘where’ clause for the update as a SQL ‘IN’, e.g. using the answer here – this will use the same newly-generated password for every post
  2. add a loop and call $wpdb->update individually for each post – this will either make a new password for each or use the same password depending on how you write the loop

    $slugs = array('page-one','page-two','page-three','page-four','page-five');
    
    global $wpdb;
    foreach( $slugs as $slug ) {
        // This bit as before
        $wpdb->update(
            $wpdb->posts,
            array( 'post_password' => uniqid() ),
            array( 'post_name'     => $slug    ),
            array( '%s' ),
            array( '%s' )
        );
    }
    
  3. just do this directly in SQL instead?

    UPDATE wp_posts
      SET  post_password = '....'
    WHERE  post_name IN ('page-one','page-two','page-three','page-four','page-five');
    

    I think this is by far the easiest, but obviously take a database backup first if you’re not confident. You’ll have to generate your own passwords somehow here though whereas the other methods will generate something random for you automatically.