Title: get_posts problem: several categories
Last modified: August 18, 2016

---

# get_posts problem: several categories

 *  Resolved [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * (@sailormarcus)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/)
 * I was using this code to display 5 posts from just one category in my sidebar,
   but when I started adding sub-categories to all posts I noticed they wouldn’t
   show up. Here’s the code:
 * `<?php $posts = get_posts( "category=3&numberposts=5" ); ?> <?php if( $posts ):?
   > <?php foreach( $posts as $post ) : setup_postdata( $post ); ?><p><div class
   ="descriptionblack"><a href="<?php the_permalink() ?>" title="Japan News Review-
   <?php the_title(); ?>">` (etc)
 * The posts are only members of one category (the sub-category) and unfortunately
   making the posts members of the base category isn’t an option (because of the
   way I’ve built the site).
 * `<?php $posts = get_posts( "category=3&numberposts=5" ); ?>`
 * this is the line I need to change, right? I bet it’s really simple, but I don’t
   know what it is I should add and how.
 * Any help would be much appreciated!

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Thread Starter [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * (@sailormarcus)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-588836)
 * I did try and change it to
 * `<?php $posts = get_posts( "category=3,20,21,22,23,24,25,26,27,28,29,30,31,32&
   numberposts=5" ); ?>`
 * (20-32 being the sub-categories I want to include in the listing)
 * but it didn’t work either.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-588838)
 * Is 3 the ID for the Category you want?
 * What happens with just:
 * `<?php $posts = get_posts( 'category=3' ); ?>`
 * or
 * `<?php $posts = get_posts( 'numberposts=5' ); ?>`
 *  Thread Starter [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * (@sailormarcus)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-588843)
 * yeah, I just want the posts from category 3, and it’s sub-categories, ie. 20-
   32 to show up and I want exactly 5 posts, from those categories.
 * only using “category=3” would give me all posts from that category, I expect (
   only I don’t have any posts with that particular number right now as all posts
   are now subcategorized)
 * numberposts=5 gives me the latest 5 blog entries, irregardless of category.
 *  Thread Starter [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * (@sailormarcus)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-588845)
 * I mean, there’s nothing malfunctioning with the code – it will give me all posts
   labelled “3”, but I want it to give me all posts labelled 3, 20, 21 etc, ie not
   just one category but several.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-588860)
 * Does it make a difference if you use ‘cat=3,20,21,22,23…’ instead of ‘category
   =3,20,21,22,23…’?
 *  Thread Starter [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * (@sailormarcus)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-588949)
 * Yes it does, it gives me the 5 latest postings (no matter category), so I guess
   it has to be “category” and not “cat” for get_posts.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-589081)
 * Okay then try this:
 *     ```
       <?php
       $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' AND $wpdb->post2cat.category_id IN (3,20,21,22,23,24,25,26,27,28,29,30,31,32) ORDER BY $wpdb->posts.post_date DESC LIMIT 5";
       $pageposts = $wpdb->get_results($querystr, OBJECT);
       ?>
       <?php if ($pageposts): ?>
       <?php foreach ($pageposts as $post): ?>
       <?php setup_postdata($post); ?>
       <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php endforeach; ?>
       <?php else : ?>
       <h2> Not Found</h2>
       <?php endif; ?>
       ```
   
 *  Thread Starter [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * (@sailormarcus)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-589142)
 * Thanks, but I ended up resolving it another way.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘get_posts problem: several categories’ is closed to new replies.

 * 8 replies
 * 2 participants
 * Last reply from: [sailormarcus](https://wordpress.org/support/users/sailormarcus/)
 * Last activity: [18 years, 10 months ago](https://wordpress.org/support/topic/get_posts-problem-several-categories/#post-589142)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
