Title: Get posts from category &#8211; exclude current post
Last modified: August 21, 2016

---

# Get posts from category – exclude current post

 *  [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/)
 * archive > single
 * While on a single post, I build a list of other posts within same category. I
   display that list in a module on the single template. The current post that I’m
   looking at is displayed in that list as the first permalink. How do I remove 
   the permalink from the list throughout my site?
 *     ```
       <ul>
                           <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                           <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 4 ); ?>
                           <?php $queryObject = new WP_Query($query); ?>
                           <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                           <li><a href="<?php echo get_permalink(); ?>"><?php if (strlen($post->post_title) > 22) { echo substr(the_title($before = '', $after = '', FALSE), 0, 22) . '...'; } else { the_title();} ?>
                             </a></li>
                           <?php endwhile; else: ?>
                           <li>No related resources found.</li>
                           <?php endif; ?>
                           <?php wp_reset_query(); ?>
                         </ul>
       ```
   
 * _[[No bumping](http://codex.wordpress.org/Forum_Welcome#No_Bumping). If it’s 
   that urgent, consider [hiring someone](http://jobs.wordpress.net/).]_

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729162)
 * you get the current post Id, for instance with `global $post; $postID = $post-
   >ID;` (you might not need the ‘global’ if you are in the single.php template);
 * and exclude the post ID from the query with `'post__not_in' => array( $postID)`
 * [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
   
   [http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters)
 * it might just work with:
 *     ```
       <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 4, 'post__not_in' => array( $post->ID ) ); ?>
       ```
   
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729207)
 * alchymyth, I appreciate your input but this does not seem to be working. When
   I’m viewing the single post, in my list module (which pulls the lates 4 post 
   in the same category) this single post is still displaying as a link even though
   I’m viewing it. This is something that is happening on a global basis.
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729208)
 * I found this back 3 years ago on here:
 *     ```
       <?php
       $yourcat = 373; // Stick the ID here and it'll be placed in the relevant places below.
   
       if( in_category( $yourcat ) ) :
       	$my_query = new WP_Query(array(
       		'showposts' => 3,
       		'orderby' => 'rand',
       		'cat' => $yourcat,
       		'post__not_in' => array($post->ID)
       	)); unset($yourcat);
       	while ($my_query->have_posts()) : $my_query->the_post();
       	?>
       		<a href="<?php the_permalink(); ?>"><img src="<?php echo p75GetThumbnail($post->ID, 100, 100); ?>"></a>
       	<?php
       	endwhile;
       endif;
       ?>
       ```
   
 * how could i incorporate this into my code:
 *     ```
       <ul class="icons">
       					  <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                             <?php $query = array ( 'post_type' => 'articles', 'articletype' => 'news', 'category__in' => $post_categories, 'posts_per_page' => 4 ); ?>
                             <?php $queryObject = new WP_Query($query); ?>
                             <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                             <li class="icon-file"><a href="/articles/<?php echo basename(get_permalink()); ?>/"><?php if (strlen($post->post_title) > 33) { echo substr(the_title($before = '', $after = '', FALSE), 0, 33) . '...'; } else { the_title();} ?>
                               </a></li>
                             <?php endwhile; else: ?>
                             <li>No research articles found.</li>
                             <?php endif; ?>
                             <?php wp_reset_query(); ?>
                           </ul>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729210)
 * > When I’m viewing the single post, in my list module (which pulls the lates 
   > 4 post in the same category) this single post is still displaying as a link
   > even though I’m viewing it
 * what code exactly did you try?
    is that ‘list module’ directly coded into single.
   php or done with a function or widget?
 * > I found this back 3 years ago on here:
 * because that code uses the same ‘post__not_in’ parameter, I would be surprised
   if that code would work where my suggestion does not work.
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729211)
 * I used this
    `<?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype'
   => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 4, 'post__not_in'
   => array( $post->ID ) ); ?>`
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729212)
 * > is that ‘list module’ directly coded into single.php or done with a function
   > or widget?
 * This is a chunk of code in my custom taxonomy single page: single-taxonomy.php
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729233)
 * a thought: would this have anything to do with the fact that I’m pulling from
   a sub category?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729234)
 * possibly, the `$post->ID` is somehow changed before you call the code, and is
   not the one from the single post.
 * try to check what it by printing it before the code;
 * `<?php echo $post->ID; ?>`
 * have you tried with `<?php global $post; ?>` ?
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729236)
 * it’s returning the proper post id – I can’t get this thing to work at all.
    Display
   all posts related to category but do not include the currently displayed post
   in the list:
 *     ```
       <ul>
                           <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                           <?php $query = array ( 'post_type' => 'community', 'category__in' => $post_categories, 'posts_per_page' => 5 ); ?>
                           <?php $queryObject = new WP_Query($query); ?>
                           <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                           <li><a href="<?php echo get_permalink(); ?>"><?php if (strlen($post->post_title) > 22) { echo substr(the_title($before = '', $after = '', FALSE), 0, 22) . '...'; } else { the_title();} ?></a></li>
                           <?php endwhile; else: ?>
                           <li>No related resources found.</li>
                           <?php endif; ?>
                           <?php wp_reset_query(); ?>
                           <li class="last"><i class="icon-circle-arrow-right"></i> <a href="/community/category/sleep/">View All Resources</a></li>
                         </ul>
       ```
   

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

The topic ‘Get posts from category – exclude current post’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/get-posts-from-category-exclude-current-post/#post-3729236)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
