Title: Passing Variable To query_posts
Last modified: August 19, 2016

---

# Passing Variable To query_posts

 *  Resolved [sintax63](https://wordpress.org/support/users/sintax63/)
 * (@sintax63)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/)
 * I am trying to pass a variable into a query_posts( ) but for some reason it doesn’t
   seem to be taking effect. I can echo the variable all over the place, and use
   it in places other than the query_posts( ), but not inside.
 * To be as brief as possible, I have a “Featured Article” on my main page, which
   is the newest post to my blog. I am trying to grab both the Category ID and Category
   Name from this and pass it down the page a bit to an “Recently In category_name”
   type of deal.
 * Here is the code from my single Featured Article area:
 *     ```
       <?php query_posts('showposts=1'); ?>
       <?php while (have_posts()) : the_post(); ?>
   
       <?php $category = get_the_category();
       $featured_NAME = $category[0]->cat_name;
        $featured_ID   = $category[0]->cat_ID;
       ?>
       ```
   
 * Now down in the “Recently In category_name” I am using the following:
 *     ```
       <?php query_posts('showposts=3&cat=$featured_ID'); ?>
       <h3>Recently In <?php echo $featured_NAME; ?></h3>
   
       <?php while (have_posts()) : the_post(); ?>
       ```
   
 * The $featured_NAME inside my <h3> tags works great, but the query_posts( ) is
   completely ignoring my $featured_ID variable. It is just reading as “cat=” and
   showing the last 3 entries in all categories.
 * I’ve read through the docs and tried numerous things, but just can’t get it to
   work.
 * Any suggestions?

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

 *  [ivovic](https://wordpress.org/support/users/ivovic/)
 * (@ivovic)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/#post-747789)
 * <?php query_posts(‘showposts=3&cat=’.$featured_ID); ?>
 * basically you want to keep the variables out of the single quotes, because anything
   inside those quotes is treated as literal text and not processed further.
 *  Thread Starter [sintax63](https://wordpress.org/support/users/sintax63/)
 * (@sintax63)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/#post-747790)
 * Works great, Ivovic –
    Thanks so much for the help!
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/#post-747791)
 * That will work. Alternatives that will also do the trick:
 * Double quoting strings makes it process variables inside the string:
    `<?php 
   query_posts("showposts=3&cat=$featured_ID"); ?>`
 * WordPress also supports explicit arrays instead of query strings:
 *     ```
       <?php query_posts(array(
       'showposts'=> 3,
       'cat'=> $featured_ID,
       )); ?>
       ```
   
 * I prefer to use arrays for everything now because it’s more explicit as to what
   is being set to what, and it’s faster as there’s less string processing going
   on.
 *  [Indojepang](https://wordpress.org/support/users/indojepang/)
 * (@indojepang)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/#post-747798)
 * Faster in whatway Otto? do you mean the regular query function is not straight
   enough?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/#post-747825)
 * Faster because all the functions in WordPress that take query string style parameters
   use arrays internally anyway.
 * See, if you call query_posts(‘foo=bar’), then it eventually calls a function 
   called [parse_str](http://php.net/parse_str) to turn ‘foo=bar’ into array(‘foo’
   =>’bar’).
 * But if you use an array in the first place, the call to parse_str never happens.
   Obviously, this is faster.

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

The topic ‘Passing Variable To query_posts’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 4 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [18 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-to-query_posts/#post-747825)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
