Title: Excluding current post from get_posts()
Last modified: August 19, 2016

---

# Excluding current post from get_posts()

 *  [jmz360](https://wordpress.org/support/users/jmz360/)
 * (@jmz360)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/excluding-current-post-from-get_posts/)
 * I’m trying to display a list of posts from a certain category in my sidebar. 
   I want to exclude the current post from the list. My code so far is:
 *     ```
       <ul>
       	<?php
       		global $post;
       		$postid = $post->ID;
       		$myposts = get_posts("category=4&exclude=".$postid."&numberposts=1&orderby=rand");
       		foreach($myposts as $post){
       	?>
   
       <li><a>"><?php the_title(); ?></a></li>
       	<?php
       		}
       	?>
       </ul>
       ```
   
 * But this doesn’t seem to work. Where am I going wrong?

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/excluding-current-post-from-get_posts/#post-1242017)
 * One possibility:
    [http://wordpress.org/support/topic/288091](http://wordpress.org/support/topic/288091)
 * or
 * 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())
       ?>
       ```
   
 *  Thread Starter [jmz360](https://wordpress.org/support/users/jmz360/)
 * (@jmz360)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/excluding-current-post-from-get_posts/#post-1242019)
 * Thanks for the quick reply, I’ll check these out.

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

The topic ‘Excluding current post from get_posts()’ is closed to new replies.

 * 2 replies
 * 2 participants
 * Last reply from: [jmz360](https://wordpress.org/support/users/jmz360/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/excluding-current-post-from-get_posts/#post-1242019)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
