2
votes

i try to realize the following: posts with a custom post type should be password-protected by default

i found this which works great https://wordpress.stackexchange.com/questions/4952/forcing-all-posts-associated-with-a-custom-post-type-to-be-private

but i dont want the posts to be private so i chanced

$post->post_status = 'private';
$post->post_password = 'some_default_password';

to

$post->post_status = 'published';
$post->post_password = 'some_default_password';

but then there is no password protection

how can i get this done?

1
Will the CPT will always be password protected? And by default do you mean when the CPT is created ("Add new cpt")? - brasofilo
yes, when a post with a CPT is added, it should be password protected - obs

1 Answers

2
votes

The following code does this, but it only works when a new post (post_type) is created.

If we don't check for empty($data['post_name']), which indicates that a new post is being created, it locks all posts with the some_default_password password.

Code based in this WordPress StackExchange Q&A.

add_filter( 'wp_insert_post_data' , 'so_13517851_filter_handler' , '99', 2 );

function so_13517851_filter_handler( $data , $postarr ) 
{
    // creating new post and correct post_type
    if( empty( $data['post_name'] ) && 'post' == $postarr['post_type'] )
        $data[ 'post_password' ] = 'some_default_password';

    return $data;
}