Title: Recent posts from current cat.   exclude categories
Last modified: August 21, 2016

---

# Recent posts from current cat. exclude categories

 *  [Dave_CL](https://wordpress.org/support/users/dave_cl/)
 * (@dave_cl)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/)
 * Hi there, I found that code and I am trying to exclude some categories ID.
    I
   use it in a single.php for most recent posts from same category like the current
   post is. But I have posts default in 2 groups of categories, lets say A,B,C,D
   and other are 1,2,3,4 … now I want to exclude all 1,2,3,4 categories and let 
   the code display only posts from A,B,C,D category – of course just one of them
   for the current one.
 *     ```
       <h3>Recently Post From Same Category</h3>
       <ul>
       <?php
       global $post;
       $category = get_the_category($post->ID);
       $category = $category[0]->cat_ID;
       $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
       foreach($myposts as $post) :
       setup_postdata($post);
       ?>
       <li>
       <a href="<?php the_permalink(); ?>">
       <?php the_title(); ?></a>
       </li>
       <?php endforeach; ?>
       <?php wp_reset_query(); ?>
       </ul>
       ```
   
 * Any idea? Thank you!
 * _[Moderator Note: [No bumping](http://codex.wordpress.org/Forum_Welcome#No_Bumping).
   If it’s that urgent, consider [hiring someone](http://directory.codepoet.com/).]_

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304258)
 * I think you can get what you want by changing this:
 *     ```
       $category = $category[0]->cat_ID;
       $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
       ```
   
 * to this:
 *     ```
       $category = $category[0]->cat_ID;
       $cat_string = "$category,-1,-2,-3,-4";
       $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'cat' => $cat_string, 'post__not_in' => array($post->ID),'post_status'=>'publish'));
       ```
   
 *  Thread Starter [Dave_CL](https://wordpress.org/support/users/dave_cl/)
 * (@dave_cl)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304271)
 * Well, the result is that there is no posts than … It seems to take just the first
   category and other just ignores.
 *  Thread Starter [Dave_CL](https://wordpress.org/support/users/dave_cl/)
 * (@dave_cl)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304272)
 * Nope… It just doesn´t give any posts either if the correct category is first 
   in alphabet.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304274)
 * Please post the code you are using.
 * When you use the ‘cat’ parameter, any positive numbers are included and any negative
   numbers are excluded. So the string ‘11,6,-42, -68’ would include category ids
   11 and 6, and exclude category ids 42 and 68.
 *  Thread Starter [Dave_CL](https://wordpress.org/support/users/dave_cl/)
 * (@dave_cl)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304275)
 * yep, i know that.. But in that code it doesn´t work properly.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304276)
 * Unless you post the code you are using, I cannot suggest anything else. Please
   put the entire template in a [pastebin](http://pastebin.com) and post a link 
   to it here.
 *  Thread Starter [Dave_CL](https://wordpress.org/support/users/dave_cl/)
 * (@dave_cl)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304277)
 * [http://pastebin.com/6umYwfxt](http://pastebin.com/6umYwfxt) 79-98
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304278)
 * Strange, this section of code worked for me (with my own category id numbers):
 *     ```
       global $post;
       $category = get_the_category($post->ID);
       $category = $category[0]->cat_ID;
       $cat_string = "$category,-15,-29,-65,-69";
       $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'cat' => $cat_string, 'post__not_in' => array($post->ID),'post_status'=>'publish'));
       foreach($myposts as $post) :
       setup_postdata($post);
       ?>
       ```
   
 * Maybe a plugin is interfering.

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

The topic ‘Recent posts from current cat. exclude categories’ is closed to new replies.

## Tags

 * ["recent post"](https://wordpress.org/support/topic-tag/recent-post/)
 * [current category](https://wordpress.org/support/topic-tag/current-category/)
 * [exclude category](https://wordpress.org/support/topic-tag/exclude-category/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/recent-posts-from-current-cat-exclude-categories/#post-4304278)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
