Last active
July 11, 2025 18:18
-
-
Save pablo-sg-pacheco/b6ebef644d93f8480b1955b4bed5b052 to your computer and use it in GitHub Desktop.
Wordpress - Password protect a custom post type programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Moriaani. Ok, for your single posts, my code was broken up into two sections, one array for Post Types and one for Categories.
In both arrays, the key (on the left) is the
name
for the PT or Term, and the value is the password in plain text.So in answer to your question:
$protected_post_types
) or you can lock down posts that are in a particularcategory
($protected_categories
).Of course, you could extend this logic to work for any taxonomy or any other post metadata.
Jerome