Title: Exclude current post when displaying recent posts in sidebar
Last modified: August 19, 2016

---

# Exclude current post when displaying recent posts in sidebar

 *  [georgerobbo](https://wordpress.org/support/users/georgerobbo/)
 * (@georgerobbo)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/exclude-current-post/)
 * Hello,
 * I’m having trouble trying to exclude the current post from my other posts sidebar.
   My code is as following.
    1.  The Main Post
 * > `
   >  <div id=”col_1″> <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?
   > > <div id=”col_1_top”> <h5>“><?php the_title(); ?></h5> </div> <div id=”col_1_bod”
   > > <p><?php the_content(); ?></p> </div> <?php endwhile; ?> <?php endif; ?> 
   > </div>
    1.  The Recent Posts
 * >  <div id=”col_2″>
   >  <div id=”col_2_top”> <h5>Other Notices</h5> </div> <div 
   > id=”col_2_bod”> <?php query_posts(‘category_name=Notice&showposts=4’); ?> <?
   > php while (have_posts()) : the_post(); ?> <div class=”col_item”> <h5>“><?php
   > the_title(); ?></h5> <p><?php the_content(); ?></p> </div> <?php endwhile; ?
   > > <?php wp_reset_query(); ?> </div> </div> `

Viewing 1 replies (of 1 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/exclude-current-post/#post-1218749)
 * Since you said “current post”…from my notes…you may find this example useful:
 * In a sidebar, if single post, for the first category of that post, display 5 
   posts in that category, excluding the single post.
 * Related:
    [http://wordpress.org/search/category+single+sidebar?forums=1](http://wordpress.org/search/category+single+sidebar?forums=1)
   [http://wordpress.org/support/topic/257858?replies=4](http://wordpress.org/support/topic/257858?replies=4)
   [http://wordpress.org/support/topic/179138?replies=4](http://wordpress.org/support/topic/179138?replies=4)
   [http://wordpress.org/support/topic/252228](http://wordpress.org/support/topic/252228)
 *     ```
       <?php
       if ( is_single() ) {
         $cats = wp_get_post_categories($post->ID);
           if ($cats) {
           $first_cat = $cats[0];
           $args=array(
             'cat' => $first_cat, //cat__not_in wouldn't work
             'post__not_in' => array($post->ID),
             'showposts'=>5,
             'caller_get_posts'=>1
           );
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
             echo 'Related Posts';
             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;
           } //if ($my_query)
         } //if ($cats)
         wp_reset_query();  // Restore global post data stomped by the_post().
       } //if (is_single())
       ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Exclude current post when displaying recent posts in sidebar’ is closed
to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/exclude-current-post/#post-1218749)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
