• Resolved atlantisliv

    (@atlantisliv)


    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)

    (@otto42)

    WordPress.org Admin

    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

    (@atlantisliv)

    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)

    (@otto42)

    WordPress.org Admin

    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

    (@atlantisliv)

    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.