Title: get_posts with excluding category
Last modified: August 21, 2016

---

# get_posts with excluding category

 *  Resolved [ventus85](https://wordpress.org/support/users/ventus85/)
 * (@ventus85)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/get_posts-with-excluding-category/)
 * Hi all.
 * The version of my WordPress is 3.5.1
 * I modified my front-page file.
    This code gets the most recent post from the 
   loop. I want to exclude a category. The id of the category is ‘6’.
 *     ```
       <?php
       $args = array( 'numberposts' => 1, 'exclude' => '6' );
       $lastposts = get_posts( $args );
       foreach($lastposts as $post) : setup_postdata($post); ?>
       	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
       	<?php the_content(); ?>
       <?php endforeach; ?>
       ```
   
 * I also tried
    `$args = array( 'numberposts' => 1, 'cat => -6' );`
 * But it does not work: in the home page show all, including the post of the excluded
   category.
 * I read [the documentation](http://codex.wordpress.org/Template_Tags/get_posts)
   and [this old topic](http://wordpress.org/support/topic/help-with-excluding-category),
   but these pages didn’t help me
 * I wonder where I am going wrong.
 * Thank you

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/get_posts-with-excluding-category/#post-3968593)
 * > I also tried
   >  `$args = array( 'numberposts' => 1, 'cat => -6' );`
 * might be a simple syntax error; change the single quotes like so:
 * `$args = array( 'numberposts' => 1, 'cat' => -6 );`
 *  Thread Starter [ventus85](https://wordpress.org/support/users/ventus85/)
 * (@ventus85)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/get_posts-with-excluding-category/#post-3968742)
 * Hi all.
 * Thank you for your answer.
 * Ops, the code is wrong in the post, but in the file it is correct, I tried
 * `$args = array( 'numberposts' => 1, 'cat' => -6 );`
 * and
 * `$args = array( 'numberposts' => 1, 'cat' => '-6' );`
 * but these solutions don’t work
 * 🙁
 *  Thread Starter [ventus85](https://wordpress.org/support/users/ventus85/)
 * (@ventus85)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/get_posts-with-excluding-category/#post-3968788)
 * `$args = array( 'numberposts' => 1, 'cat' => -6 );`
 * The code is correct.
    Yesterday when I wrote this code, maybe I must have been
   sleeping 😛
 * Thank you!

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

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

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [ventus85](https://wordpress.org/support/users/ventus85/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/get_posts-with-excluding-category/#post-3968788)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
