Title: Exclude Posts from &quot;Recent Posts&quot; Section
Last modified: August 30, 2016

---

# Exclude Posts from "Recent Posts" Section

 *  [cjv17](https://wordpress.org/support/users/cjv17/)
 * (@cjv17)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/)
 * I am trying to exclude certain posts from a “recent posts” section loaded on 
   my homepage. I would like to exclude them by tag, but by ID or category would
   be alright as well (in that order of preference). I assume this is the code I
   should be editing:
 *     ```
       <?php
                   $limit = get_option('posts_per_page');
                   $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                   query_posts('showposts=' . $limit . '&paged=' . $paged);
                   $wp_query->is_archive = true;
                   $wp_query->is_home = false;
                   ?>
       ```
   
 * but cannot figure out what to change. Link to site: [http://harvardelp.wpengine.com/welcome/](http://harvardelp.wpengine.com/welcome/)
   where I’m hoping to filter out the posts beneath the slider (specifically, to
   exclude anything included in the slider). Any help would be appreciated, thanks.

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

 *  [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * (@craig-ralston)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336697)
 * It is recommended to no longer use `query_posts()` and switch to [WP_Query](https://codex.wordpress.org/Class_Reference/WP_Query)
   instead but to answer your question:
 * To exclude a category using your existing code, just update the following line
   where 1 is the ID of the category you want to exclude.
 * `query_posts('showposts=' . $limit . '&paged=' . $paged . '&cat=-1');`
 *  Thread Starter [cjv17](https://wordpress.org/support/users/cjv17/)
 * (@cjv17)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336698)
 * Thank you! Is there a similar option for selecting based on tags?
 *  [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * (@craig-ralston)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336699)
 * You should be able to include the [tag__not_in](https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters)
   parameter in your query.
 *  Thread Starter [cjv17](https://wordpress.org/support/users/cjv17/)
 * (@cjv17)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336700)
 * I tried that —
 * I included:
 *     ```
       $exclude = get_term_by('slug','ExcludeThisTag','post_tag');
       $exclusion = 'tag_not_in' => array($exclude->term_id);
       query_posts('showsposts=' . $limit . '&paged=' . $paged . $exclusion);
       ```
   
 * Sorry, I do not have much experience with PHP. Do you know the mistake I am making
   here?
 *  [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * (@craig-ralston)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336701)
 * I am not in a position to test it currently (I can in a little while if the issue
   still persists) but the first thing I’m noticing is it looks like you are missing
   an ampersand and an underscore.
 * `$exclusion = 'tag_not_in' => array($exclude->term_id);`
 * should be
 * `$exclusion = '&tag__not_in' => array($exclude->term_id);`
 *  Thread Starter [cjv17](https://wordpress.org/support/users/cjv17/)
 * (@cjv17)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336702)
 * Woops — I typed those here incorrectly, but it is actually correct in my code.
   Nonetheless, that code seems to crash the page. Not sure why, however.
 *  [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * (@craig-ralston)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336704)
 * Let’s just do this the recommended way. I just tested the following tag exclusion
   and it is working fine for me.
 *     ```
       $exclude = get_term_by( 'slug', 'ExcludeThisTag', 'post_tag' );
       $newquery = new WP_Query();
       $newquery->query( array( 'tag__not_in' => $exclude->term_id ) );
   
       while ( $newquery->have_posts() ) : $newquery->the_post(); ?>
           // your loop stuff here
       <?php endwhile; ?>
       ```
   
 *  Thread Starter [cjv17](https://wordpress.org/support/users/cjv17/)
 * (@cjv17)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336706)
 * I’ll give this a shot, thank you. I am using the Infoway theme and it was using
   query_posts, and I was trying to avoid changing too much, but I trust that this
   will work better.
 *  [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * (@craig-ralston)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336724)
 * Just create a [child theme](https://codex.wordpress.org/Child_Themes) and you
   won’t lose your changes when you update the theme.
 *  [heyjones](https://wordpress.org/support/users/heyjones/)
 * (@heyjones)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336814)
 * The post__not_in function isn’t working in 4.2.3.
 *  [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * (@craig-ralston)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336817)
 * **[@heyjones](https://wordpress.org/support/users/heyjones/)** Hey! Welcome to
   the forums, please start your own thread so both of your issues can be addressed
   individually. I would be more than happy to assist you there. Make sure you are
   passing an array of post id(s). The following is working perfectly fine for me
   on 4.2.3.
 * **Exclude post with id of 219**
 *     ```
       $exclude_ids = array( '219' );
       $new_query = new WP_Query();
       $new_query->query( array( 'post__not_in' => $exclude_ids ) );
       ```
   

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

The topic ‘Exclude Posts from "Recent Posts" Section’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 3 participants
 * Last reply from: [Craig Ralston](https://wordpress.org/support/users/craig-ralston/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/exclude-posts-from-recent-posts-section/#post-6336817)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
