Title: Exclude posts from WP_Query?
Last modified: August 19, 2016

---

# Exclude posts from WP_Query?

 *  [christopherross](https://wordpress.org/support/users/christopherross/)
 * (@christopherross)
 * [17 years ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/)
 * I’m trying to fetch a list of posts on a page but exclude some specific id’s 
   from the results, so I’ve tried
 * `$my_query = new WP_Query('showposts=10&amp;exclude=1,24,312')`
 * but the resulting object still contains the excluded posts. I’ve also tried:
 * `$my_query = new WP_Query('showposts=10&amp;p=-1,-24,-312')`
 * to no avail. Can anybody suggest the proper way to do this?
 * **For example** … I want to fetch the 10 most recent posts in the list except
   for the posts with an id’s of 1,24,312.

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

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [17 years ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080231)
 * hi
 * ‘post__not_in’ => array(6,2,8)
    – exclusion, lets you specify the post IDs NOT
   to retrieve
 * see [http://codex.wordpress.org/Template_Tags/query_posts#Post_.26_Page_Parameters](http://codex.wordpress.org/Template_Tags/query_posts#Post_.26_Page_Parameters)
 * also, to confirm, use a single ampersand between parameters, not the “ampersand
   amp;” that the forum software displays a ampersand character as.
 *  [desertmagic](https://wordpress.org/support/users/desertmagic/)
 * (@desertmagic)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080467)
 * You were really close. Use cat= followed by a negative sign and whatever category
   id you want to exclude.
 * new WP_Query(“showposts=10&cat=-22,-12,-6”)
 *  [trefethen](https://wordpress.org/support/users/trefethen/)
 * (@trefethen)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080488)
 * Can this exclusion be used as a text widget in the sidebar? I am trying to show
   specific category posts in the sidebar widget “Recent Posts” while excluding 
   posts within other categories.
 * Is there any easier way to go about this?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080489)
 * Consider downloading and installing [Otto’s PHP Code Widget](http://wordpress.org/extend/plugins/php-code-widget/),
   then put something like this there:
 *     ```
       <?php
       $args=array(
         'category__not_in' => array(22,12,6,4),
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => 5,
         'caller_get_posts'=> 1
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_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;
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  [trefethen](https://wordpress.org/support/users/trefethen/)
 * (@trefethen)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080490)
 * Thank you Michael.
 * Would I replace the 22,12,6,4 within the () with the categories that I want to
   include in the sidebar?
 * ~ John
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080492)
 * To exclude categories 22,12,6,4
    `'category__not_in' => array(22,12,6,4),`
 * To include categories 7,6,8,32
    `'category__in' => array(7,6,8,32),`

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

The topic ‘Exclude posts from WP_Query?’ is closed to new replies.

## Tags

 * [exclude](https://wordpress.org/support/topic-tag/exclude/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 5 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query/#post-1080492)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
