This is basic operation in WordPress, but it’s not as straightforward as you might think. For that, you will usually use get_posts() function that allows you to set category, but this function can use many more options than actually documented, you can use all WP Query arguments with it.

We will see how to exclude child categories from results and how to do a bit more with some other additional arguments this function can use.

Basic category settings in get_posts()

If you follow get_posts() documented arguments, without going any deeper, you will see that you can specify category by ID:

<?php
$posts = get_posts(array('category' => 1));
?>

And this will get you 5 posts from specify category and ALL child categories belonging to that one! If you need more than 5 or if you need all, you must set number of posts to get:

<?php
$posts = get_posts(array('numberposts' => 10000, 'category' => 1));
?>

This will get you up to 10.000 posts from category with ID 1 and ALL child categories belonging to it.

Additional categories arguments

But, in many cases you don’t want posts belonging to child categories, you want posts that are specifically assigned to a category. Well, in that case this method is no good. Or is it? Well, you can still use same function, but this time with arguments that were not documented for that function.

<?php
$posts = get_posts(array('numberposts' => 10000, 'category__in' => array(1)));
?>

Now we are getting somewhere. This will limit results only to category with ID 1, and will ignore any child categories this one might have. You can specify more than one category as a value for category__in argument and that will get you posts that belong to at least one of the categories in array.

There are few more arguments that you will find interesting for getting posts in categories.

<?php
$posts = get_posts(array('category__and' => array(1, 5, 42)));
$posts = get_posts(array('category__not_in' => array(3, 71)));
?>

Line 2 uses category__and argument, and this one will get you posts belonging to all specified categories. If a post belongs to 1 and 5 only and not 42 it will not be used. Only posts belonging to 1, 5 and 42 are returned.

Line 3 uses category__not_in argument, and this will return posts not belonging to specified categories. In all this cases, you must specify the category ID’s.

And more

For a full list of arguments you can use with get_posts() function, you can check out this article from Codex:
http://codex.wordpress.org/Function_Reference/query_posts

6 Responses to “Get posts for a category”

  1. Timmy | November 9, 2010 at 6:58 pm

    Thanks for this code! Helped me alot!

  2. Webartdesign | July 1, 2012 at 11:08 pm

    Thank alot… Just what i was looking for. A Great time saver for my wordpress website…

    Ciao Brian

  3. sasa | July 16, 2012 at 7:30 am

    I have one post in two categories , how to show up only post from one categories ?

  4. kundan | November 22, 2012 at 11:04 am

    thanks a lot, this post is so useful to me, but still there one problem.
    the post count is increase every day near 100 to 150 in my database, so is there any way to numberofpost = ‘unlimited’ ?

  5. Celso | December 24, 2012 at 9:18 pm

    Thanks! After lot of search in Google I found this blog.

    God bless you

Leave a Reply

Dev4Press Plugins Pack

Dev4Press Plugins Pack

Personal$99.00
Business$249.00
Developer$499.00
GD bbPress Toolbox

GD bbPress Toolbox

Personal$30.00
Business$90.00
Developer$180.00
GD Custom Posts And Taxonomies Tools

GD Custom Posts And Taxonomies Tools

Personal$35.00
Business$105.00
Developer$210.00
GD Press Tools

GD Press Tools

Personal$40.00
Business$120.00
Developer$240.00
GD Products Center

GD Products Center

Personal$40.00
Business$120.00
Developer$240.00
xScape Theme Club

xScape Theme Club

Standard$149.00