Support » Plugins » Retrieve posts from multiple categories

  • charismabiz

    (@charismabiz)


    Hi,

    I would like to retrieve posts from multiple categories (eg: all posts with category IDs 5 and 17). How would I do this?

Viewing 15 replies - 1 through 15 (of 18 total)
  • MichaelH

    (@michaelh)

    The Loop article in Codex has a Exclude posts from some Category that you could use with a few changes.

    charismabiz

    (@charismabiz)

    I’m not using it within the loop… making a custom list on the side.

    Thanks for the help tho. I appreciate the effort 🙂

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    If you’re retrieving posts, you’re always making a Loop. Maybe not THE Loop, but it’s a Loop nevertheless.

    charismabiz

    (@charismabiz)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    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.

    charismabiz

    (@charismabiz)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    since I can only pass 1 category ID to WP_Query?

    Says who?

    ...new WP_Query('cat=5,17&showposts=5');

    charismabiz

    (@charismabiz)

    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!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

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

    WPChina

    (@wordpresschina)

    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?

    WPChina

    (@wordpresschina)

    Any idea how I can retrieve posts from multiple categories, but only limit them to posts from the last X days?

    WPChina

    (@wordpresschina)

    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.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Retrieve posts from multiple categories’ is closed to new replies.