Title: query_posts for category
Last modified: August 21, 2016

---

# query_posts for category

 *  Resolved [Connor Crosby](https://wordpress.org/support/users/ccmovies/)
 * (@ccmovies)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/query_posts-for-category/)
 * I am working on a theme that uses query_posts. For the category page, I am trying
   to show posts that are just in that category. I tried the following code that
   I found on this [page](http://stackoverflow.com/questions/12310066/wordpress-query-postscategory-name-get-the-title-wont-return-posts-for-c)
   but it does not work.
 *     ```
       <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   
       // Replace any spaces in the title with dashes
       $title_slug = strtolower(str_replace( " ", "-" , the_title_attribute('echo=0') ) );
       // Get the category object by using our new title slug
       $id_obj = get_category_by_slug($title_slug);
       // Get the category id so that we can use that in the query
       $cat_id = $id_obj->term_id;
   
       query_posts('post_type=post&posts_per_page=5&cat='.$cat_id.'&paged=' . $paged); ?>
       ```
   
 * Any suggestions? I would really appreciate it.

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/query_posts-for-category/#post-4083553)
 * > For the category page, I am trying to show posts that are just in that category.
 * the cat ID of a category archive page is:
 * `get_query_var( 'cat' );`
 * does the ‘just in that category’ refer to the fact that common category archive
   pages will also show posts of the child categories?
 * in that case, possibly try:
 *     ```
       global $query_string;
       query_posts( $query_string . '&cat='. get_query_var( 'cat' ) );
       ```
   
 * [http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters](http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters)
 *  Thread Starter [Connor Crosby](https://wordpress.org/support/users/ccmovies/)
 * (@ccmovies)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/query_posts-for-category/#post-4083629)
 * That seemed to do the trick! Thank you very much for your help.
 *  [paulwpxp](https://wordpress.org/support/users/paulwp/)
 * (@paulwp)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/query_posts-for-category/#post-4083633)
 * I never know before that category page also show posts from its child category.
   I have always been using 1 category per post, and many tags.

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

The topic ‘query_posts for category’ is closed to new replies.

## Tags

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

 * 3 replies
 * 3 participants
 * Last reply from: [paulwpxp](https://wordpress.org/support/users/paulwp/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/query_posts-for-category/#post-4083633)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
