0

I would like to create a page template that will display all of the posts by the currently logged in user. I managed to find an older thread which contained this code:

if ( is_user_logged_in() ):

global $current_user;
get_currentuserinfo();
$author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts()) : $author_posts->the_post();
?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>       
<?php           
endwhile;

else :

echo "not logged in";

endif;

however, the code does not appear to work (in fact, any page using the template which contains it simply won't load).

  • 3
    that code works fine for me, enable debugging and check for errors. – Milo Nov 10 '13 at 16:00
  • @Milo: Would you mind posting the complete code you're using for the page template that works? I have a feeling I've made a coding error somewhere and I would like to compare. – Zyniker Nov 10 '13 at 16:25
  • 1
    I used your code and nothing else, except an opening <?php tag before it. if you've made an error, enabling debugging will tell you what it is and on what line. – Milo Nov 10 '13 at 16:27
0

Working code:

    <?php if (is_user_logged_in() ) : ?>

        <?php
        //if a certain page, then display posts authored by the logged in user
        $page_title = 'Support History';
        if ( is_user_logged_in()  && is_page($page_title) ) {
        global $current_user;
        get_currentuserinfo();
        $args=array(
            'author' => $current_user->ID,
            'post_type' => 'post',
            'post_status' => 'publish, private',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
        echo 'Your Support History';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        endwhile;
        }
        wp_reset_query();  // Restore global post data stomped by the_post().
        }
        ?>

    <?php endif; ?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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