Title: Problems with using variables in php
Last modified: August 19, 2016

---

# Problems with using variables in php

 *  Resolved [brownca](https://wordpress.org/support/users/brownca/)
 * (@brownca)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/)
 * I’m having problems with a piece of code that sits in the sidebar of my posts.
   The code is supposed to get the category parent ID, assign it to a variable, 
   and then use that variable when calling for the most recent posts from the category.
 *     ```
       <?php
       foreach((get_the_category()) as $category) {
           $parent_id = $category->category_parent . ' ';
       }; ?>
   
       <?php echo $parent_id; ?>
   
       <?php if(is_single()) {
       	$my_query = new WP_Query('cat=$parent_id&order=desc&showposts=7'); ?>
       <h2>Related Stories</h2>
       <?php
       	while ($my_query->have_posts()) : $my_query->the_post();
       	$do_not_duplicate = $post->ID; ?>
   
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       <br />
       <?php endwhile; }
   
       ?>
       ```
   
 * The cat=%parent_id bit doesn’t seem to work (after $my_query = new WP_Query).
   When I replace %parent_id with (for example) 3, it correctly displays posts with
   category id 3. When I echo %parent_id it prints the correct category of the post,(
   for example – 3).
 * So there’s really no reason why it shouldn’t be working, unless there’s something
   obvious I’ve missed. Any help will be very gratefully received. Thank you.

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235506)
 * Get category ID of first category on the single post
 *     ```
       if ( is_single() ) {
       $category = get_the_category();
       $category_id = $category[0]->cat_ID;
       }
       ```
   
 *  Thread Starter [brownca](https://wordpress.org/support/users/brownca/)
 * (@brownca)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235526)
 * That doesn’t make any difference – the only difference that should make is getting
   the category id instead of the parent category id. Unfortunately it does neither,
   and just returns all posts regardless of category.
 * I’m pretty sure it’s something to do with using a $variable in line 9 of my code
   above, since if I replace the variable after ” ‘cat= ” with a number of a category,
   the code works fine.
 * I just don’t know why it won’t work with a variable containing a value, but it
   will work with the value on its own.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235534)
 * What about
 *     ```
       if ( is_single() ) {
       $category = get_the_category();
       $category_id = $category[0]->cat_ID;
       echo $category[0]->parent;
       }
       ```
   
 *  Thread Starter [brownca](https://wordpress.org/support/users/brownca/)
 * (@brownca)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235646)
 * That part of the code isn’t the problem. I’ve already successfully stored the
   parent category ID, which I know because I checked with:
    `<?php echo $parent_id;?
   >` When I’m viewing a post with category parent id 3, I will see the number 3
   in the sidebar, so I know the variable should contain the number 3.
 * The problem is this line:
    `$my_query = new WP_Query('cat=$parent_id&order=desc&
   showposts=7'); ?>` specifically the **cat=$parent_id** part. This doesn’t seem
   to be looking inside the variable $parent_id for the number 3, because it just
   returns posts from all categories. When I replace $parent_id with the number 
   3, the code works. But it shouldn’t make any difference, if you see what I mean?
 *  Thread Starter [brownca](https://wordpress.org/support/users/brownca/)
 * (@brownca)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235726)
 * Well, I’ve found a workaround for now by using an If statement for every single
   category, which makes for tons more code but it gets the job done. If anyone 
   knows what I was doing wrong before with the variable I’d love to know.
 * Thanks for your help MichaelH 🙂
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235741)
 * It’s because of the single quotes, you need to concatenate the string when using
   single quotes, where as double quotes allow literal variables in the string..
 *     ```
       $my_query = new WP_Query('cat=$parent_id&order=desc&showposts=7'); ?>
       ```
   
 * fixed version.
 *     ```
       $my_query = new WP_Query('cat='.$parent_id.'&order=desc&showposts=7'); ?>
       ```
   
 * or with double quotes.
 *     ```
       $my_query = new WP_Query("cat=$parent_id&order=desc&showposts=7"); ?>
       ```
   
 * Using a good editor with syntax highlighting makes these kind of errors easy 
   to spot.
 *  Thread Starter [brownca](https://wordpress.org/support/users/brownca/)
 * (@brownca)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235742)
 * Brilliant. Thanks very much, I’ll download a proper editor now to avoid mistakes
   like these in the future. Many thanks.

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

The topic ‘Problems with using variables in php’ is closed to new replies.

 * 7 replies
 * 3 participants
 * Last reply from: [brownca](https://wordpress.org/support/users/brownca/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/problems-with-using-variables-in-php/#post-1235742)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
