Title: Excluding a category using get_posts
Last modified: August 18, 2016

---

# Excluding a category using get_posts

 *  Resolved [atlantisliv](https://wordpress.org/support/users/atlantisliv/)
 * (@atlantisliv)
 * [19 years, 3 months ago](https://wordpress.org/support/topic/excluding-a-category-using-get_posts/)
 * I want to list the 5 most recent posts in all categories except category 4. If
   I do this:
 *     ```
       <?php
        $posts = get_posts('numberposts=5&exclude=4');
        foreach($posts as $post) :
        ?>
       ```
   
 * … that simply excludes the post with id=4.
 * I thought “exclude” was supposed to exclude categories, not posts. Maybe this
   is a conflict caused by a plugin? Or is there another way?

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

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 3 months ago](https://wordpress.org/support/topic/excluding-a-category-using-get_posts/#post-533325)
 * Nope, exclude and include work on post ID’s there, not on categories.
 * There is no way to exclude a category with get_posts.
 *  Thread Starter [atlantisliv](https://wordpress.org/support/users/atlantisliv/)
 * (@atlantisliv)
 * [19 years, 3 months ago](https://wordpress.org/support/topic/excluding-a-category-using-get_posts/#post-533467)
 * Ah… Is there a way I can exclude more than a single post, then? Thing is, I don’t
   want to make some backdated posts appear in the “Recent” section. So it’s a temporary
   thing.
 * Or maybe limit get_posts to just a few categories? (I don’t have many on my blog)
 * Thanks for the help.
 * Edit: Wait a minute… if I backdate them, they won’t appear in “Recent” at all.(
   Doh!) But if there’s a way to exclude/limit certain cats/posts I’d still appreciate
   it 😉
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 3 months ago](https://wordpress.org/support/topic/excluding-a-category-using-get_posts/#post-533469)
 * The exclude parameter takes a comma separated list of post IDs to exclude. Like
   so:
 * `get_posts('exclude=4,8,15,16,23,42');`
 * There is a category parameter, but it’s limited to a single category. get_posts()
   is weak in general. It’s useful for one shot scenarios, but if you want anything
   complicated, you’re better off creating a WP_Query object and writing yourself
   a Loop to use the output from it.
 *  Thread Starter [atlantisliv](https://wordpress.org/support/users/atlantisliv/)
 * (@atlantisliv)
 * [19 years, 3 months ago](https://wordpress.org/support/topic/excluding-a-category-using-get_posts/#post-533488)
 * Okay, thank you.
 * I’ll read up on wp_query.

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

The topic ‘Excluding a category using get_posts’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [atlantisliv](https://wordpress.org/support/users/atlantisliv/)
 * Last activity: [19 years, 3 months ago](https://wordpress.org/support/topic/excluding-a-category-using-get_posts/#post-533488)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
