search ]

Set Default Post Status to Private in WordPress

The following code lets you set the default post status on your WordPress site to Private.

<?php 
function default_posts_to_private()
{
	global $post;

	if ( $post->post_status == 'publish' ) {
		$visibility = 'public';
		$visibility_trans = __('Public');
	} elseif ( !empty( $post->post_password ) ) {
		$visibility = 'password';
		$visibility_trans = __('Password protected');
	} elseif ( $post->post_type == 'post' && is_sticky( $post->ID ) ) {
		$visibility = 'public';
		$visibility_trans = __('Public, Sticky');
	} else {
		$post->post_password = '';
		$visibility = 'private';
		$visibility_trans = __('Private');
	} 
?>

	<script type="text/javascript">
		(function($){
			try {
				$('#post-visibility-display').text('<?php echo $visibility_trans; ?>');
				$('#hidden-post-visibility').val('<?php echo $visibility; ?>');
				$('#visibility-radio-<?php echo $visibility; ?>').attr('checked', true);
			} catch(err){}
		}) (jQuery);
	</script>
	<?php
add_action( 'post_submitbox_misc_actions' , 'default_posts_to_private' );

This uses a WordPress hook. Learn more in WordPress Hooks Explained.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo