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.

Does anyone know of a way to add code to the functions.php file which will automatically force all posts belonging to a custom post type to be "private" and/or "password protected" with a default password set?

I am specifically referring to creating a new post or editing an existing post thus ensuring a post belonging to a specific custom post type never changes...

share|improve this question
    
Do you want to prevent editing, but allow public viewing? –  Jan Fabry Dec 6 '10 at 22:50
add comment

3 Answers

up vote 6 down vote accepted

You can hook onto save_post, wp_insert_post or wp_insert_post_data to modify the post object prior to it being inserted or saved.

Using save_post or wp_insert_post the callback would need to declare two args and would receive the post object as the second incoming variable.. (and i'm showing you to cover the alternatives, TheDeadMedic's example would be fine to).

For setting default values for a particular post type for new posts you can use a small hack by hooking onto default_content (although default_title would also work to), like the example i gave here.

You essentially need two functions, one to modify post objects at the time of save/insert and one to set default post object values, here's an example of the two necessary functions(again noting you can swap my save_post callback for the example already given by TheDeadMedic).

add_action( 'save_post', 'check_type_values', 10, 2 );

function check_type_values( $post_id, $post ) {

    if( $post->post_type )
        switch( $post->post_type ) {
            case 'my_custom_type':
                $post->post_status = 'private';
                $post->post_password = ( '' == $post->post_password ) ? 'some_default_when_no_password' : $post->post_password;
            break;
        }   
    return;
}

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 'my_custom_type':
                $post->post_status = 'private';
                $post->post_password = 'some_default_password';
            break;
        }
    return $post_content;
}

Hope that helps...

share|improve this answer
    
Nice one for going the extra mile :) Although I would advise using wp_insert_post_data over wp_insert_post (the latter merely gets fired once the post is saved, and you'd need to invoke a save again if you wanted to change data, whilst the former allows you to change the post data, before it's saved to the database). –  TheDeadMedic Dec 6 '10 at 23:48
    
Good point, my answer was mainly intended to compliment your own... just wanted to make sure the "setting default values" aspect was covered(noone noticed my answer in the other topic), and figured i might aswell show how to use one of the other hooks whilst i'm at it.. :) –  t31os Dec 7 '10 at 0:18
    
very cool... thanks the both of you! –  NetConstructor.com Dec 7 '10 at 6:02
    
@TheDealMedic and @t31os -- quick question on this. Based on the Medics comment, where would "wp_insert_post" or "wp_insert_post_data" be used within the code? Would you mind modifying the answer with the correct code so that its easily usable by others? –  NetConstructor.com Dec 20 '10 at 7:33
    
@TheDealMedic and @t31os -- Also, in testing this code further I noticed that if an existing post (which is not private) is edited through the backend and then subsequently saved the post status does not change to "private". Although I can see situations where only new posts get a default value of "private" for my current situation I would like to enforce that all posts for this post type are "private" and where this option can not be modified by any users other than the administrator. How can this be accomplished? –  NetConstructor.com Dec 20 '10 at 7:39
show 2 more comments
function force_type_private($post)
{
    if ($post['post_type'] != 'my_post_type' || $post['post_status'] == 'trash')
        return $post;

    $post['post_password'] = 'my password';
    $post['post_status'] = 'private';
    return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');
share|improve this answer
1  
I wouldn't add the $post['post_password'] section. There's a significant different between private posts and password-protected posts. –  EAMann Dec 7 '10 at 19:06
    
@EAMann It was mainly for demonstrative purposes, but I guess I should have mentioned that :) –  TheDeadMedic Dec 8 '10 at 10:58
    
@EAmann -- EAMann, I consider you to be a true expert in this community and I am curious to hear your feedback on each of the proposed solutions (if anyone else has feedback please also comment)... What do you feel is the best approach. I ask because this answer seems simple and clean and has 3 votes where as the previous answer I accepted has only two votes and seems more complex but possibly comprehensive for different situations. Let's assume we are just focusing on setting all posts to "private" and eliminate the password protected option. –  NetConstructor.com Dec 20 '10 at 7:22
    
@TheDealMedic or @EAMann -- Also, in testing your code further it does seem that your solution seems to be a better alternative to the previous one I accepted so I accepted this answer as the correct one. Could you extend your code to show how the option to modify the "visibility" can be removed and also hopefully show how the visibility can be edited/changed if the user logged in is an admin or has that capability? –  NetConstructor.com Dec 20 '10 at 7:49
add comment

Couldn't we just make a template page for the custom post type and do a check if the is the user is logged in and check if the user is a certain role? Lets take for example you want to have a post-type that is only to be viewed by the admins:

<?php if ( is_user_logged_in() && is_admin() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; endif; ?>
share|improve this answer
    
davis, you are mis-reading the Question, this is about the backend (/wp-admin/)... –  brasofilo Jan 21 '13 at 3:16
add comment

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.