Forcing all posts associated with a custom post type to be private

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…

Leave a Comment