Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Let's say that we have a category with slug protected_category and some posts in it. Is there a way to force all posts in that category to be password protected using a default password?

UPDATE: I found a solution how to set a default password to all posts in a custom post type:

add_filter( 'default_content', 'set_default_values', 10, 2 );
function set_default_values( $post_content, $post ) {
    if ( $post->post_type )
        switch ( $post->post_type ) {
            case 'protected_post':
                $post->post_status = 'protected';
                $post->post_password = '123123';
                break;
        }

    return $post_content; 
}

So how can I modify this to target a specific category?

share|improve this question
    
You've not given information about your setup, but generally this is possible. Please extend your question with sufficient information, what's your approach, what've you tried so far? Besides that, take a look at Forcing all posts associated with a custom post type to be private, which is of course is about making a specific post type private, but the methods can be adopted for your needs. –  ialocin Feb 20 at 20:51
    
Thanks for the reply. I updated my question. –  NoSense Feb 20 at 21:00

1 Answer 1

up vote 1 down vote accepted

This should do what you want:

add_filter('save_post', 'wpse_135377_protect_post');
function wpse_135377_protect_post($post_ID) {
    if (in_array(
        'protected_category',
        wp_get_post_categories($post_ID, array('fields' => 'slugs'))
    )) {
        // we have to remove the function
        // as it will be called by `wp_update_post` (=> infinite loop)
        remove_filter('save_post', 'wpse_135377_protect_post');
        wp_update_post(array(
            'ID' => $post_ID,
            'post_password' => '123123',
        ));
        // now we add it again
        add_filter('save_post', 'wpse_135377_protect_post');
    }
} // function wpse_135377_protect_post

There were several problems in your code base:

  • there is no post status protected—password protected posts have the post status publish as well (and they have a password);
  • you should use the save_post action (as suggested in the below comment and the linked answer);
  • the $post object that is passed into the functions (save_post as well as default_content) is not a pointer (reference) so anything you do to the object stays within the function (unless you insert it into the db).

Happy protecting!

share|improve this answer
    
It doesn't work :(...Any ideas where I'm wrong? –  NoSense Feb 20 at 21:13
    
I've tried the last but it still doesn't work... –  NoSense Feb 20 at 21:16
    
I would be very grateful if you could help me :( –  NoSense Feb 21 at 0:04
1  
maybe you should try save_post hook instead –  Jesse Feb 21 at 1:21
1  
Sorry for the late reply...Thank you @t f; with very little modifications to suite my theme now works like a charm :) –  NoSense Feb 21 at 18:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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