Dev4Press» posts Tag Archives, | Dev4Press http://www.dev4press.com Premium Plugins and Themes for WordPress Thu, 09 Feb 2012 20:24:04 +0000 en hourly 1 Get posts for a categoryhttp://www.dev4press.com/2010/tutorials/wordpress/tips/get-posts-for-a-category/ http://www.dev4press.com/2010/tutorials/wordpress/tips/get-posts-for-a-category/#comments Sat, 25 Sep 2010 10:57:08 +0000 MillaN http://www.dev4press.com/?p=3254
Rating: 0.0/5 (0 votes cast)


]]>
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


Rating: 0.0/5 (0 votes cast)


]]>
http://www.dev4press.com/2010/tutorials/wordpress/tips/get-posts-for-a-category/feed/ 1