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

I am creating a custom post type, for which I want all posts to be password-protected with the same password.

I realize that I can set a password on a post-by-post basis from within the admin panel, however this seems tedious and prone to error/forgetfulness.

Read More

I’m sure there’s a way to make this happen with some kind of filter, but I just don’t know where to start.

Any help would be greatly appreciated. Thanks!

Related posts

1 comment

  1. 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.

Comments are closed.