Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a password-protected page, for whom I'd like to use a custom template to query posts from a specific category. How can I achieve that query_posts should work only after users submit the password of the page? I'm trying with this but doesn't work (page only displays header and footer)

<?php
/*
Template Name: xxxx
*/
?>

        <?php get_header(); ?>
            <?php if ( post_password_required() ) : ?>
            <?php query_posts('cat=9'); ?>
            <?php while ( have_posts() ) : the_post(); ?>


            <a href="<?php the_permalink(); ?>"><h1 style="padding-top:0;margin-top:-5px" class="entry-title"><?php the_title(); ?></h1></a>
                <div class="entry-content" style="padding:0"><?php the_content(); ?></div>
                                        <div class="entry-meta">
                    <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
                    </div>


            <?php endwhile; ?>
            <?php endif; ?>


        <?php get_footer(); ?>
share|improve this question
    
Solved with this code :) pastebin.com/HkH9UyBF –  MultiformeIngegno Nov 6 '12 at 12:29
    
So the answer stays hidden on an external site? Shall I close the question, or will you add an answer here? –  toscho Nov 6 '12 at 13:27
    
Uhm, I don't have enough rep to answer my questions.. –  MultiformeIngegno Nov 6 '12 at 17:52
add comment

2 Answers 2

up vote 0 down vote accepted

separate the queries for the current page and the query for all posts in a category.
Untested example:

<?php
/*
Template Name: xxxx
*/
?>

<?php while ( have_posts() ) : the_post(); ?>
<?php get_header(); ?>
<?php if ( post_password_required() ) :
    $args = array(
        'cat' => 9,
        'numberposts' => -1,
        );
    $posts = get_posts( $args );
    foreach( $posts as $post ) : setup_postdata($post); ?>
        <a href="<?php the_permalink(); ?>">
            <h1 style="padding-top:0;margin-top:-5px" class="entry-title"><?php the_title(); ?></h1>
        </a>
        <div class="entry-content" style="padding:0"><?php the_content(); ?></div>
        <div class="entry-meta">
            <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
        </div>
    <?php endforeach; ?>

<?php endif; ?>
<?php get_footer(); ?>
<?php endwhile; ?>
share|improve this answer
    
I think this is wrong : the code inside if ( post_password_required() ) block will be executed only if the user is not authenticated. –  Fabien Quatravaux Oct 17 '13 at 13:44
    
post_password_required() returns true if post requires password and correct password has been provided. See codex.wordpress.org/Function_Reference/post_password_required –  windyjonas Oct 18 '13 at 11:12
    
I'm sorry, but you should read the doc twice : return value : False if a password is not required or the correct password cookie is present, true otherwise. It means that if the user is authenticated (password has been provided before and the auth cookie is present) it will return false. I admit that the codex formulation is confusing, but the behavior is the expected one. If you have already provided the password, it is not required anymore! –  Fabien Quatravaux Oct 18 '13 at 14:19
    
Another formulation would be : return value : true if a password is required and has not been verified, false otherwise –  Fabien Quatravaux Oct 18 '13 at 14:22
add comment

Your solution is good MultiformeIngegno, but you should use the WP template tag get_the_password_form() instead of harcoding the password form, like shown in the Codex :

<?php
/*
Template Name: xxxx
*/
?>
    <?php get_header(); ?>
        <?php if ( post_password_required() ) : 

            // ask for the password
            echo get_the_password_form(); 
        else: 

            // authenticated user, display the content
        ?>
        <?php query_posts('cat=9'); ?>
        <?php while ( have_posts() ) : the_post(); ?>


        <a href="<?php the_permalink(); ?>"><h1 style="padding-top:0;margin-top:-5px" class="entry-title"><?php the_title(); ?></h1></a>
            <div class="entry-content" style="padding:0"><?php the_content(); ?></div>
                                    <div class="entry-meta">
                <?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
                </div>


        <?php endwhile; ?>
        <?php endif; ?>


    <?php get_footer(); ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.