Title: Querying Posts Using Categories
Last modified: August 19, 2016

---

# Querying Posts Using Categories

 *  Resolved [nospario](https://wordpress.org/support/users/nospario/)
 * (@nospario)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/querying-posts-using-categories/)
 * Hi,
 * I want to return posts dependant on the categories they are in, is this possible?
 * For example:
 * post1 is associated with 2 categories, category_name1 and category_name2
 * post2 is associated with 2 categories, category_name1 and category_name3
 * post3 is associated with 2 categories, category_name2 and category_name3
 * I only want to return post1 and not post2 or post3 querying only on category_name.
 * I’ve tried this but it’s not working as I had expected.
 *     ```
       //The Query
       			$cat1 = get_cat_ID($category_name1);
       			$cat2 = get_cat_ID($category_name2);
       			$args = array('cat' => $cat1 . ',' .$cat2);
       			query_posts($args);
       			//The Loop
       			if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       				<?php the_content(); ?>
       			<?php
       			endwhile;
       			endif;
       			//Reset Query
       			wp_reset_query();
       ```
   
 * Thanks.

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

 *  [transom](https://wordpress.org/support/users/transom/)
 * (@transom)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/querying-posts-using-categories/#post-2029827)
 * You want use “category__and” (double-underlines) e.g.
    array( ‘category__and’
   => array( #, #) )
 *  Thread Starter [nospario](https://wordpress.org/support/users/nospario/)
 * (@nospario)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/querying-posts-using-categories/#post-2029830)
 * Thanks transom.
 * That works when I hard code the values in but not when I use the variables. Any
   idea what I’m doing wrong?
 * `query_posts( array( 'category__and' => array($cat1.','.$cat2)));`
 *  [fonglh](https://wordpress.org/support/users/fonglh/)
 * (@fonglh)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/querying-posts-using-categories/#post-2029836)
 * `array($cat1, $cat2)`
 * not joined together as a string
 *  Thread Starter [nospario](https://wordpress.org/support/users/nospario/)
 * (@nospario)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/querying-posts-using-categories/#post-2029837)
 * That doesn’t seem to do it but I found this [post](http://wordpress.org/support/topic/getting-query_postarraycategory_and-gt-to-work-with-variables-not-values?replies=28)
   which has helped me sort it.
 * Thanks.

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

The topic ‘Querying Posts Using Categories’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [nospario](https://wordpress.org/support/users/nospario/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/querying-posts-using-categories/#post-2029837)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
