Title: Simple query_posts question
Last modified: August 19, 2016

---

# Simple query_posts question

 *  [altoidboy](https://wordpress.org/support/users/altoidboy/)
 * (@altoidboy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/)
 * I’m trying to get query_posts to refer to a category slug that I’ve got stored
   in $mycatslug. So I insert the following code:
 *     ```
       print $mycatslug;
       query_posts( 'category_name = $mycatslug' . '&paged=' . get_query_var('paged'));
       ```
   
 * The first line works correctly — I see that it has printed the category slug 
   on the site. But the second line does not — this should restrict it to only the
   category slug mentioned on the first line. Did I do something wrong in its syntax?

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

 *  [Chinmoy](https://wordpress.org/support/users/chinmoy/)
 * (@chinmoy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906373)
 *     ```
       $paged = get_query_var('paged') ? get_query_var('paged') : 1;
       query_posts( 'category_name ='. $mycatslug . '&paged=' . $paged);
       ```
   
 * Try once it
 * Chinmoy
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906385)
 * You need to use double quotes when you have a PHP variable within a quoted string.
 * `query_posts("category_name = $mycatslug&paged=". get_query_var('paged'));`
 *  Thread Starter [altoidboy](https://wordpress.org/support/users/altoidboy/)
 * (@altoidboy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906530)
 * Thanks very much for the help. Unfortunately, neither worked.
 * I guess I should mention that I’m trying to mod a template (JournalCrunch) that
   doesn’t seem to support pagination properly, so I’m trying to write the correct
   query_post so that it does. My full relevant portion of code is this:
 *     ```
       <?php
       $catslug = get_query_var('cat');
       $mycatslug = get_category ($cat)->slug;
       print $mycatslug;  // this is just here as a temporary diagnostic and it does correctly return the category slug
       query_posts("category_name = $mycatslug&paged=". get_query_var('paged'));
       ?>
       ```
   
 * When I put this in the code, the resultant page displays ALL posts, not posts
   of this specific category.
 * However, if I replace the last line with a hardcoded category, like this:
 * `query_posts( 'cat=4' . '&paged=' . get_query_var('paged'));`
 * then the same resultant page does correctly display posts from category #4.
 * Any thoughts?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906537)
 * Try it with:
 *     ```
       <?php
       $catID = get_query_var('cat');
       $paged = get_query_var('paged') ? get_query_var('paged') : 1;
       query_posts('cat='. $catID . '&paged='. $paged);
       ?>
       ```
   
 *  Thread Starter [altoidboy](https://wordpress.org/support/users/altoidboy/)
 * (@altoidboy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906560)
 * keesiemeijer, that appears to work!! I can’t thank you enough for helping me 
   figure that out — this was a major stumbling block to getting this site finished.
 * I’ve tested it successfully on the category-specific pages so far; next I’ll 
   try and migrate the same solution to the home page.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906563)
 * You’re welcome. Success on trying it on the home page.

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

The topic ‘Simple query_posts question’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/simple-query_posts-question/#post-1906563)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
