Title: List Current Users Recent Posts In Sidebar
Last modified: August 19, 2016

---

# List Current Users Recent Posts In Sidebar

 *  [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/)
 * Something I have been battling with for a while now and need some help.
 * I would like to be able to list the current users (who is logged in) posts in
   the sidebar and possibly limit this to say 6 posts?
 * is this possible at all?
 * I’ve been trying to take some code from author.php and adding it to my rightsidebar.
   php but with no luck.
 * Any suggestions on how I can do this?
 * Thanks!

Viewing 13 replies - 1 through 13 (of 13 total)

 *  [Edward Caissie](https://wordpress.org/support/users/cais/)
 * (@cais)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1595742)
 * Maybe try something like this:
 *     ```
       <?php
       if ( is_user_logged_in() ) {
         $user_id = get_current_user_id();
         query_posts( "author=$user_id&posts_per_page=6" );
         // insert your loop code here
         wp_reset_query();
       } else {
         // display non-logged in user content
       }
       ?>
       ```
   
 * You can likely copy the_Loop code from your theme and paste it in where I wrote
   the ‘// insert your loop code here’ comment.
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1595914)
 * Thanks Cais.
 * This is my final code.
 *     ```
       <?php
       if ( is_user_logged_in() ) {
         $user_id = get_current_user_id();
         query_posts( "author=$user_id&posts_per_page=6" );?>
   
         <ul>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
               <li>
                   <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                   <?php the_title(); ?></a>
               </li>
   
           <?php endwhile; else: ?>
       </ul>
               <p><?php _e('You Have No Submissions'); ?></p>
   
           <?php endif; ?>
       <?php
         wp_reset_query();
       } else {
        ('You Have No Submissions.');
       }
       ?>
       ```
   
 * And it works great!
 * I wonder though how can I add a message to display when someone is not logged
   in. “Such as please login to view your recent posts”
 *  [Edward Caissie](https://wordpress.org/support/users/cais/)
 * (@cais)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1595929)
 * I’m glad it is working for you …
 * To add a message … try this at the end of your code instead of what you are using:
 *     ```
       ...
         wp_reset_query();
       } else {
         echo "Please login to view your recent posts.";
       }
       ?>
       ```
   
 * You can modify the message inside the quotes to what suits your needs best.
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1595946)
 * perfect!
 * I just had the thought ho can this be altered to show the current users recent
   comments. There isn’t a query_comments tag is there?
 * I’m guessing it will have something to with <?php wp_list_comments( $args ); ?
   > perhaps?
 * Thanks for your support!
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596009)
 * right I found this [http://wordpress.org/support/topic/312462](http://wordpress.org/support/topic/312462)
   and it has pointed me in the right direction but I am not quite there.
 * The code
 *     ```
       <?
       if(get_query_var('author_name')) :
       $curauth = get_userdatabylogin(get_query_var('author_name'));
       else :
       $curauth = get_userdata(get_query_var('author'));
       endif;
       $querystr = "
           SELECT comment_ID, comment_post_ID, post_title, comment_content
           FROM $wpdb->comments, $wpdb->posts
           WHERE user_id = $curauth->ID
           AND comment_post_id = ID
           AND comment_approved = 1
           ORDER BY comment_ID DESC
        ";
   
        $comments_array = $wpdb->get_results($querystr, OBJECT);
   
       if ($comments_array): ?>
       <h2>Recent Comments </h2>
       <ul>
       <? foreach ($comments_array as $comment):
       	setup_postdata($comment);
       	echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_post_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
       endforeach; ?>
       </ul>
       <? endif; ?>
       ```
   
 * Works great on the authors page but I’d like to alter it so that it works in 
   the sidebar and gets the recent comments of the logged in user or displays a 
   message “please log in to view your recent comments”
 * Thanks
 *  [Edward Caissie](https://wordpress.org/support/users/cais/)
 * (@cais)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596033)
 * The following is only quickly tested … user beware.
 * I just quickly combined all of the above examples, I would expect it to work 
   but you should make sure to test it thoroughly.
 *     ```
       <?php
       if ( is_user_logged_in() ) {
         $user_id = get_current_user_id();
   
         $querystr = "
           SELECT comment_ID, comment_post_ID, post_title, comment_content
           FROM $wpdb->comments, $wpdb->posts
           WHERE user_id = $user_id
           AND comment_post_id = ID
           AND comment_approved = 1
           ORDER BY comment_ID DESC
           ";
   
         $comments_array = $wpdb->get_results($querystr, OBJECT);
   
         if ($comments_array):
         ?>
           <h2>Recent Comments </h2>
           <ul>
             <?php foreach ($comments_array as $comment):
               setup_postdata($comment);
               echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_post_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
             endforeach; ?>
           </ul>
       <?php endif;
       } else {
         echo "Please login to view your recent comments.";
       }
       ?>
       ```
   
 * PS: There may be a lot of code clean-up involved as _WP\_DEBUG set to ‘true’_
   throws quite a few “Notices” that should be addressed.
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596038)
 * Thanks. This does work in my page templates but not within my side bar.
 * I’ve tried adding it as PHP code within a widget and also directly pasting it
   with my rightsidebar.php but nothing shows. Not even the <h2>Recent Comments 
   </h2>
 *  [Edward Caissie](https://wordpress.org/support/users/cais/)
 * (@cais)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596046)
 * I just copy and pasted the code above into the sidebar on one of my test sites
   and it appears to work fine (with the exception of the WP_DEBUG Notices). Both
   logged in and logged out.
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596051)
 * OK – I’ve been scratching my head for this now for ages!
 * When I am logged out I receive the message “Please login to view your recent 
   comments” [See link](http://www.creativityds.com) but when I log in I see nothing
   below the heading Your Recent Comments so no list of comments or the other two
   widgets placed below this.
 * I did run the debug and the message I received the error _Undefined variable:
   wpdb _ (I dont think this really affects anything).
 * Just to also mention that when I end the code with
 * `<?php endif;?>`
 * and not
 *     ```
       <?php endif;
       } else {
         echo "Please login to view your recent comments.";
       }
       ?>
       ```
   
 * The other widgets below show up but no comments are listed.
 * I’m going to keep trying if I solve this I will let you know!
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596081)
 * still no luck with this.
    I’ve tried combining the two codes but with the same
   results.
 * Strange how it works in a page template and not the sidebar!
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596156)
 * I’ve been back on this last night but still with zero luck.
 * Anyone with any suggestions?
 *  [shodezignz](https://wordpress.org/support/users/shodezignz/)
 * (@shodezignz)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596211)
 * I have just tried the show current users recent posts and my page displays nothing
   now? Was there something else in your code that you did or changed to make it
   work?
 *  Thread Starter [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * (@aminabbasian)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596218)
 * nope not changed any of the coding.
 * It might be an idea to double check your mark up.

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘List Current Users Recent Posts In Sidebar’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 13 replies
 * 3 participants
 * Last reply from: [aminabbasian](https://wordpress.org/support/users/aminabbasian/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/list-current-users-recent-posts-in-sidebar/#post-1596218)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
