Skip to content

Instantly share code, notes, and snippets.

@pablo-sg-pacheco
Last active July 11, 2025 18:18
Show Gist options
  • Save pablo-sg-pacheco/b6ebef644d93f8480b1955b4bed5b052 to your computer and use it in GitHub Desktop.
Save pablo-sg-pacheco/b6ebef644d93f8480b1955b4bed5b052 to your computer and use it in GitHub Desktop.
Wordpress - Password protect a custom post type programmatically
<?php
/**
* Password protects a custom post type programmatically.
*
* @param $post
*/
add_action( 'the_post', function( $post ){
if ( $post->post_type != 'post' ) {
return;
}
$post->post_password = 'your_password';
} );
?>
@Moriaani
Copy link

Thank you sooo much, @jerome-toole

Works like a charm ๐Ÿ‘๐Ÿ™‚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment