1

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.

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!

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.

6
  • Thanks I will try this and see! But will this only apply to already-published posts? The goal is to have this feature applied to new posts as they are being created. (Having it applied to previously-published posts would be a nice bonus though.) – Evster Jan 20 '14 at 20:01
  • it will be applicable to all the posts, new and already existing :) – Maruti Mohanty Jan 20 '14 at 20:04
  • Excellent, thank you! I will try it and report back. – Evster Jan 20 '14 at 20:14
  • I was thinking about another way to solve this problem which does not involve direct database manipulation. Would it be possible to set up a filter using "password_edit_pre" and have it automatically apply a password to only posts from within the custom post type inside the WP admin panel when these posts are being created? (See codex.wordpress.org/Plugin_API/Filter_Reference/… ) I think I'd prefer this solution if possible. I realize this method will not apply a password to already-published posts, but that's not much of a concern because this is a brand new site. – Evster Jan 21 '14 at 0:53
  • Yes we can look for a filter to do so, and regarding your concern about the password being applied to the previous posts, then I have tested it and it applies to all the post previous and new. – Maruti Mohanty Jan 21 '14 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.