Easiest way to make post private by default

I don’t know how to make a plugin so I can’t do what’s suggested here How can I make it so the Add New Post page has Visibility set to Private by default?

so what’s alternative ?

4 Answers
4

Accepted solution is not the correct answer to change the visibility of any post type to any status. Below code is the right way to change the post status.

function set_post_type_status_private( $status, $post_id ) {
    $status="private";
    return $status;
}
add_filter( 'status_edit_pre', 'set_post_type_status_private', 10, 2 );

Updated:

The above filter will change the post status to Private when user will hit Save Draft or Publish button. So on edit page load if you see Status Public then don’t worry.

There is one more filter available to change the status before saving into database. The filter is status_save_pre but I didn’t find any documentation on this page so I wrote below code to test it.

function save_post_type_status_private( $status ) {
    $status="private";
    return $status;
}
add_filter( 'status_save_pre', 'save_post_type_status_private', 10, 1 );

The above filter saves the post as Private post type as soon as edit page load so one may want to use this filter over status_edit_pre but if I use status_save_pre filter I run into an issue, I can’t delete any post. So I prefer ‘status_edit_pre’ over ‘status_save_pre’ until WordPress team fix this bug.

Leave a Comment