Can I override the content array using the_posts filter?
The the_content
filter will work for this. This filter allows you to alter the output of the_content()
and get_the_content()
:
add_filter( 'the_content', 'wpse_members_only', 20 );
function wpse_members_only( $content ) {
// If user is not logged in, show restricted content message.
// Change this conditional statement based on how you want to check for
// membership status. I'd suggest using capabilites, e.g. current_user_can( $capability , $object_id );
if ( ! is_user_logged_in() ) {
$content = __( 'Sorry, this content is reserved for members only.', 'text-domain' );
}
return $content;
}