Hi,
I would like to retrieve posts from multiple categories (eg: all posts with category IDs 5 and 17). How would I do this?
Hi,
I would like to retrieve posts from multiple categories (eg: all posts with category IDs 5 and 17). How would I do this?
The Loop article in Codex has a Exclude posts from some Category that you could use with a few changes.
I'm not using it within the loop... making a custom list on the side.
Thanks for the help tho. I appreciate the effort :)
If you're retrieving posts, you're always making a Loop. Maybe not THE Loop, but it's a Loop nevertheless.
Yep... but I thought "The Loop" was the main post retrieval loop?
I've just rehecked the documentation at http://codex.wordpress.org/The_Loop
The section provided by MichaelH is very useful, with a simple modification it would work as needed. However, the main question is how do I use this "the loop" for a sidebar module? Since "The Loop" deals only with posts/pages that will show up on that current page instead of ALL posts.
You just need a different kind of loop. This would be the best way to do it:
<?php $my_query = new WP_Query('showposts=5&whatever=whatever');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<!-- Do stuff... -->
<?php endwhile; ?>
Basically, it's a separate loop with some query parameters that you define directly.
Mmmm, won't that be very inefficient tho, since I can only pass 1 category ID to WP_Query?
eg: 6000 posts, 2000 that match one category, of which 400 match the second category. that would retrieve 2000 posts (can't retrieve less because say you want 5 posts. you won't know that the first Z will contain 5 that also match the second category condition), then have to go through it to get the X posts.
since I can only pass 1 category ID to WP_Query?
Says who?
...new WP_Query('cat=5,17&showposts=5');
Oh... ohhhh... wow, thanks buddy!
Is there any documentation to WP_Query? The ones I found (http://codex.wordpress.org/Function_Reference/WP_Query and http://codex.wordpress.org/User:DavidHouse/Wordpress_Code_Flow) doesn't seem too helpful.
Specifically, does that display posts that match BOTH category 5 and 17, or is it "5 or 17"?
Also, how do I get the category ID that it is in, when in the loop?
Thanks again!
The WP_Query constructor takes the same arguments as query_posts() does.
And I think it gets any posts in "either 5 or 17", actually. There does not appear to be any way to make it get posts in "both 5 and 17".
To get category information for a post in the loop, use get_the_category().
hey charismabiz,
i had the same need as you and i managed to show only posts that were under two categories. i adapted the code from the following:
http://codex.wordpress.org/Template_Tags/query_posts#Excluding_Multiple_Categories
it's kinda dumb solution: i first queried one category with query_posts() and then narrowed to the posts that also were under the second category with the conditional in_category() placed inside the loop.
doing this way, wp will only show the overlapping subgroup of posts that are under both categories :)
since I can only pass 1 category ID to WP_Query?
Says who?
...new WP_Query('cat=5,17&showposts=5');
This was exactly what i needed, thank you!!
One more thing... I have asked this on other threads without a response over last few months and so I hope this is topical here:
Now that I can retrieve posts from multiple categories, how do I limit it to only posts from the last X days? for example I want the last 7 days... any ideas?
Any idea how I can retrieve posts from multiple categories, but only limit them to posts from the last X days?
Sorry, but any idea how I can retrieve posts from multiple categories, but only limit them to posts from the last X days?
I can't find codex info and I'm sure this is a need for others!
why is this place using such a poorly laid out forum? 90% of posts i've looked up in order to find an answer come to conclusions exactly like this thread. completely unanswered.
Yes, it would be good if I could figure out how to get X days.
Posts on this forum seem to disappear from view after a while, so only a good Google search brings back old posts and unanswered queries from long ago... :(
Ok, I finally figured out how to do multiple categories. Just pre-load your $posts var by using
query_posts(array('category__and'=>array(3,4,8)))
Where 3, 4, and 8 are the categories you want. Then call the loop like normal and magic.
Anyway, I have a whole write-up on it with code that shows how to make the categories multi-selectable and such here:
http://www.jeremyduffy.com/computers-internet/wordpress-stuff/wordpress-multiple-categories/
You must log in to post.