Title: Help with excluding category
Last modified: August 19, 2016

---

# Help with excluding category

 *  Resolved [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/)
 * Right now this code gets the most recent post from the loop,
    and display the
   title and thumbnail..
 * Is it possible to exclude a category, from this function?
 * Heres my code:
 *     ```
       <a href="<?php the_permalink(); ?>">
       	<?php get_the_post_thumbnail($post->ID); ?>
       	<div id="newestpost" class="bg50 absolute">
       	<h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
       	</div>
       </a>
       ```
   

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994450)
 * [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994452)
 * [@esmi](https://wordpress.org/support/users/esmi/)
    I’ve tried that already, 
   but i could’nt make it work..
 * Perhaps you could give me an example?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994459)
 * See [this Codex page](http://codex.wordpress.org/Function_Reference/query_posts#Exclude_Categories_From_Your_Home_Page)
   for an example using query_posts.
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994468)
 * It doesnt work..
 * The code is placed outside the loop..
    Wich makes it a bit more difficult..
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994471)
 * Sorry? The code snippet you posted above clearly shows part of the Loop.
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994477)
 * It is actually in the header.php..
 * It displays the most recent post in the topbanner.
    And the most recent post 
   is then removed from the loop with: `<?php query_posts('posts_per_page=6&offset
   =1'); ?>`
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994488)
 * In that case, you have multiple Loops on the 1 page, so you would be better off
   using get_posts or WP_Query for the secondary queries. Same arguments but get_posts
   uses a foreach loop.
 * [http://codex.wordpress.org/Function_Reference/get_posts](http://codex.wordpress.org/Function_Reference/get_posts)
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994500)
 * Ok, now it looks like this:
 *     ```
       <?php
       $args = array( 'numberposts' => 1, 'orderby' => 'recent' );
       $rand_posts = get_posts( $args );
       foreach( $rand_posts as $post ) : ?>
       	<li><a href="<?php the_permalink(); ?>">
       	<?php the_post_thumbnail(); ?>
       	<div id="newestpost" class="bg50 absolute">
       	<h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
       	</div></a></li>
       <?php endforeach; ?>
       ```
   
 * Now, what do i do to exclude a category?
    It doesnt seem to work if i use: `<?
   php query_posts( '&cat=-7,-8,-9'); ?>`
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994537)
 * `$args = array( 'numberposts' => 1, 'orderby' => 'recent', 'cat => -7,-8,-9' );`
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994720)
 * Wow! that simple..
 * Thanks for the help, i really appreciate it! 🙂
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994726)
 * Glad it’s working now. 🙂
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994731)
 * Just found out that the new code causes the next_post_link and previous_post_link
   to not work properly..
 * If i change the “`$rand_posts as $post`“-part to for example, “`$posts as $post`”
   the tags work again, but then the code doesnt work.. 🙁
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994838)
 * Try adding `<?php wp_reset_query();?>` after `<?php endforeach; ?>`
 *  Thread Starter [sclausen](https://wordpress.org/support/users/sclausen/)
 * (@sclausen)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994843)
 * Its works now 🙂 Thank you 🙂

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

The topic ‘Help with excluding category’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [sclausen](https://wordpress.org/support/users/sclausen/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/help-with-excluding-category/#post-1994843)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
