Title: Passing a variable to query_posts
Last modified: August 19, 2016

---

# Passing a variable to query_posts

 *  Resolved [ianb-1](https://wordpress.org/support/users/ianb-1/)
 * (@ianb-1)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/passing-a-variable-to-query_posts/)
 * I’m trying to build a page template for a set of custom archive page templates.
   There’s going to be a bunch, so I am trying to write a single template that I
   can pass a variable set in a page custom field that sets what category_name is.
   I’ve got all the other pieces working, but I can’t get the $category variable
   to do what it needs to. I’m sure this is totally basic, but I’m stumped right
   now.
 *     ```
       <?php
              $category = get_post_meta($post->ID, "CategoryName", true);
               		if ($category){
       				query_posts('showposts=10&category_name=echo $category');
       				}
               		?>
       ```
   

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

 *  [jon](https://wordpress.org/support/users/adiant/)
 * (@adiant)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/passing-a-variable-to-query_posts/#post-1185879)
 * Single and double quotes work differently. Only double quotes will evaluate a
   variable.
 * There may be other problems, but this should clean up that one:
    `query_posts("
   showposts=10&category_name=$category");`
 * `query_posts('showposts=10&category_name=' . $category);` would also work.
 * The `if ($category)` coding also caught my eye. If it doesn’t work, try
    `if (
   $category != "")`
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/passing-a-variable-to-query_posts/#post-1185881)
 * You might want to check the category exists before you pass it into the query_posts,
   else query_posts will just grab posts that don’t belong to the category given(
   it will ignore the category parameter if not given a valid category), it’ll proberly
   end up giving you all posts..
 *     ```
       if ($category != "")
       ```
   
 * Not sure how that will help..
 *     ```
       if ($category)
       ```
   
 * Is fine for the usage above..
 *  Thread Starter [ianb-1](https://wordpress.org/support/users/ianb-1/)
 * (@ianb-1)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/passing-a-variable-to-query_posts/#post-1185962)
 * Thank you both. I’ve got my issue sorted out now. I knew it was something simple
   like that – but it was Friday and my brain had stopped.

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

The topic ‘Passing a variable to query_posts’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [ianb-1](https://wordpress.org/support/users/ianb-1/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/passing-a-variable-to-query_posts/#post-1185962)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
