Title: Fail query_posts on multiple categories
Last modified: August 19, 2016

---

# Fail query_posts on multiple categories

 *  Resolved [Leovenous](https://wordpress.org/support/users/leovenous/)
 * (@leovenous)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/)
 * For a form I am populating a combo-box with:
 * `<?php query_posts('category_name=upcoming-trips'); ?>`
 * This works fine until I try another category:
 * `<?php query_posts('category_name=upcoming-trips,closed'); ?>`
 * Then the whole query fails, and I get no results.
 * What am I doing wrong?

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

 *  [Chinmoy](https://wordpress.org/support/users/chinmoy/)
 * (@chinmoy)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1902995)
 * `<?php query_posts('category_name=array("upcoming-trips","closed")'); ?>`
 * try this.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1902998)
 * > What am I doing wrong?
 * you haven’t read the codex.
 * [http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters](http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters)
 *  Thread Starter [Leovenous](https://wordpress.org/support/users/leovenous/)
 * (@leovenous)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903026)
 * > you haven’t read the codex.
 * Yes I had. Several times. And I experimented with it. This is why I hardly every
   post in the WP forums. Are you all so burnt out by the lame-o’s that you have
   to be pissy to everyone?
 * > Display posts that have these categories, using category slug:
   > > `query_posts( 'category_name=staff,news' );`
 * How is what I did different from the codex? I’m using category slugs separated
   by a comma.
 * Chinmoy29 I tried that and it didn’t worked. Tanked the whole query again.
 *  Thread Starter [Leovenous](https://wordpress.org/support/users/leovenous/)
 * (@leovenous)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903030)
 * Here is the whole section if it helps:
 * > `<p><select name=”trip” multiple size=”6″>
   >  <?php query_posts(‘category_name
   > =upcoming-trips’); ?> <? while (have_posts()) : the_post(); ?> <option value
   > =”<?php the_title(); ?>”><?php the_title(); ?></option> <?php endwhile; ?> 
   > <option value=”No Specific Trip”>No Specific Trip</option> </select></p>`
 *  [g3legacy](https://wordpress.org/support/users/g3legacy/)
 * (@g3legacy)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903037)
 * Hi, in the codex it has that example:
    `query_posts( 'category_name=staff,news');`
   It says the category slug, are “upcoming-trips,closed” the category slugs? If
   not i’ll try something.
 * Dan
 *  [g3legacy](https://wordpress.org/support/users/g3legacy/)
 * (@g3legacy)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903038)
 * Oh, hang on…
    `<? while (have_posts()) : the_post(); ?>` Should be; `<?php while(
   have_posts()) : the_post(); ?>`
 *  Thread Starter [Leovenous](https://wordpress.org/support/users/leovenous/)
 * (@leovenous)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903047)
 * Hey I used the category numbers method and it works. I don’t see why the slugs
   wouldn’t work, but it is what it is. Thanks for your input folks.
 * BTW g3legacy, for non-outputting php5 its okay to have `<?` only, but it was 
   an accident in this case.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903051)
 * > Yes I had. Several times. And I experimented with it.
 * my apology – you are right.
    up to recently it was known fact that you cannot
   use more than one category slug with ‘category_name’.
 * the codex must have been edited recently, and this wrong information must have
   been added (i can only guess that being able to use more than one category slug
   might be planned for wp3.1 (?))
 * you probably have to switch to category ids to show posts of more than one category;
   
   to make this more automated, get the category id from the category slug:
 *     ```
       <?php $cat_slug = 'closed';
       $caty = get_term_by('slug', $cat_slug, 'category' );
       $cat_id = $caty->term_id; ?>
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_term_by](http://codex.wordpress.org/Function_Reference/get_term_by)

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

The topic ‘Fail query_posts on multiple categories’ 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/)
 * 8 replies
 * 4 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/fail-query_posts-on-multiple-categories/#post-1903051)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
