How to automatically apply a password to all posts within a custom post type

If you want to make all the posts for a post type password protected with the same password then you can do run an update query like below to make this happen.

Use the following code in the active theme’s functions.php file.

global $wpdb;
$wpdb->update( 
    $wpdb->prefix . 'posts', 
    array( 'post_password' => 'wpse' ), // Replace wpse with your password 
    array( 'post_type' => 'post' ), // Replace post with the custom post type
    array( '%s' ), 
    array( '%d' ) 
);

The above will make all the post type post password protected with the password as wpse.

1 thought on “How to automatically apply a password to all posts within a custom post type”

  1. Everything is very open with a really clear clarification of the challenges.
    It was definitely informative. Your site is useful.
    Thank you for sharing!

    Reply

Leave a Comment