Title: query_posts, get post by second category
Last modified: August 19, 2016

---

# query_posts, get post by second category

 *  [Alain](https://wordpress.org/support/users/alainotjens/)
 * (@alainotjens)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-get-post-by-second-category/)
 * In my sidebar I am trying to retrieve a list of posts from a certain category
   with query_posts.
 *     ```
       $query = "cat=5&showposts=3";
       $posts = query_posts($query);
       ```
   
 * This is working well except for posts that are assigned to more than one category.
 * For example:
    Post a,b,c are in category 5 and post d is in category 4 and 5.
   Now my query doesn’t return post d.
 * Is there a way to make this work?
 * By the way these posts with multiple categories do get displayed when queried
   in the normal loop for example when I use [http://www.domainname.com/?cat=5](http://www.domainname.com/?cat=5)

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-get-post-by-second-category/#post-1366023)
 *     ```
       <?php
       $args=array(
         'category__in' => array(4,5),
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => -1,
         'caller_get_posts'=> 1
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         echo 'List of Posts';
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
         endwhile;
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 * For posts that belong to category 4 and 5 only see [category__and parameter](http://codex.wordpress.org/Template_Tags/query_posts#Category_Parameters)
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-get-post-by-second-category/#post-1366082)
 * > showposts=3
 * if your post d with cat=4,5 is number four in the list, it will not show.

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

The topic ‘query_posts, get post by second category’ is closed to new replies.

## Tags

 * [multiple categories](https://wordpress.org/support/topic-tag/multiple-categories/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 3 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-get-post-by-second-category/#post-1366082)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
