Title: query posts from 2 categories
Last modified: August 19, 2016

---

# query posts from 2 categories

 *  [zedesino](https://wordpress.org/support/users/zedesino/)
 * (@zedesino)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/)
 * Hello everybody, one more advanced question.
 * i have 2 groups of categories, one is list of cities and 1 is OFFERS, NEWS, TIPS.
 * I have some queries in sideabar in 3 groups news, offers, tips but i want when
   i click on some city, NY for example to show me only news that have selected 
   NY category and NEWS category.
 * is it possible.
    when i use cat=x,y shows me all posts from both categories, 
   and i need to show me only posts that have selected category x and y not only
   x or only y.
 * thanks

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

 *  [krembo99](https://wordpress.org/support/users/krembo99/)
 * (@krembo99)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/#post-943440)
 * well.. It is possible to do in several ways, one of which is the [in_category](http://codex.wordpress.org/Template_Tags/in_category)
   tag, but I think the best thing would be to order that FROM THE ROOT, by applying
   the right categories and tags to each post, and then query also tags in combination
   to categories (if you have a LOT of cities) , or if you have not so many, separate
   the categories in a hierarchy.
    But first look at the in_category tag..
 *  [krembo99](https://wordpress.org/support/users/krembo99/)
 * (@krembo99)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/#post-943442)
 *     ```
       <?php
       $recent = new WP_Query("cat=X,Y"); while($recent->have_posts()) : $recent->the_post();
   
       	if (in_category(X) && in_category(Y)) { ?>
       		<div class="myclass">
       			<h3><?php the_title(); ?></h3>
       			<?php the_content(); ?>
       		</div>
       <?php }; ?>
       <?php endwhile; ?>
       ```
   
 * Now, If you want to add a limit to the number of posts :
 *     ```
       <?php
       $recent = new WP_Query("cat=X,Y"); while($recent->have_posts()) : $recent->the_post();
   
       static $limitcount = 0;
   
       if ($limitcount == "1") { break; }
       else {
       	if (in_category(X) && in_category(Y)) { ?>
       		<div class="myclass">
       			<h3><?php the_title(); ?></h3>
       			<?php the_content(); ?>
       		</div>
       		<?php $limitcount++;
       	};
       }; ?>
       <?php endwhile; ?>
       ```
   
 * you can also use
    `if ($counter == "2") { break; }` to set another limit. and`
   if (in_category(X) && in_category(Y) && in_category(Z))` if you want to cross
   check more than 2 categories
 *  Thread Starter [zedesino](https://wordpress.org/support/users/zedesino/)
 * (@zedesino)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/#post-943545)
 * hello krembo99,
    i tryed your solution but doesnt work. I want to show this posts
   when i’m viewing page in wp, so i’m not in_category and doesnt work. but thanks
   anyway for time you spent
 *  Thread Starter [zedesino](https://wordpress.org/support/users/zedesino/)
 * (@zedesino)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/#post-943546)
 * i founded faster than i was thinking
    query_posts(array(‘category__and’ => array(
   3,20)));
 * [here](http://codex.wordpress.org/Template_Tags/query_posts)
 *  [krembo99](https://wordpress.org/support/users/krembo99/)
 * (@krembo99)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/#post-943571)
 * The solution works if you implement it the right way…
    The only thing is , that
   maybe I did not understood what exactly you need.

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

The topic ‘query posts from 2 categories’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [krembo99](https://wordpress.org/support/users/krembo99/)
 * Last activity: [17 years, 5 months ago](https://wordpress.org/support/topic/query-posts-from-2-categories/#post-943571)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
