Password protecting not working for custom post type single-template.php? – WordPress

I have created a custom post-type ‘Clients’ where admin user can create new clients, add pictures and details to post, then password protect the page so only a particular client can access the content.

For displaying content of this post-type on the front end, I’m using a single-clients.php template. It displays the content perfectly, but the password protect function does not display the form and hide the content, even if I’m in a different browser, cache cleared/logged out of WordPress (viewing it as a regular end-user would).

Read More

What might I be doing wrong here?

<?php get_header(); ?>

  <div class="container-client">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      Display all fields and content for post-type

    <?php endif; ?>

    <?php endwhile; else: ?>

    <div class="alert-box error">Sorry, this page no longer exists :( &mdash; <a href="<?php bloginfo('url'); ?>/">Back to home</a></div>

    <?php endif; ?>

  </div>

<?php get_footer(); ?>

This is roughly how my single-clients.php page is setup. Is there any way to manually display the password function so that when end-user visits page, the content is hidden and password form is displayed?

Related posts

Leave a Reply

1 comment

  1. I had exactly this problem and after some trying and reading the codex I came up with this solution:

    <?php
    add_filter('single_template', function($single_template) {
        global $post;
    
        if ($post -> post_type == 'your-custom-post-type') {
            if (!post_password_required()) {
                $single_template = 'path-to-your/single-your-custom-post-type.php';
            }
        }
    
        return $single_template;
    });
    ?>
    

    This way the page is only rendered in the custom sinlge view after entering a password, if it is password protected.