Title: Using a variable inside WP_Query
Last modified: August 19, 2016

---

# Using a variable inside WP_Query

 *  [thejester12](https://wordpress.org/support/users/thejester12/)
 * (@thejester12)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-a-variable-inside-wp_query/)
 * Howdy, wondering how I can use a php variable that I have predefined in the middle
   of a wp_query statement? Basically I’m creating a custom “related posts” kind
   of thing that finds posts with shared tags. (Only one specific tag is stored 
   in the variable.) Here’s the code I’m working with below:
 * `<?php $posts_query2 = new WP_Query('tag=news+$myvariable&paged=$paged&showposts
   =4'); ?>`
 * The statement works fine when I take away the variable and test it by putting
   in the exact statement that I want to be there. I also know the variable is working
   fine by doing an echo statement in the post.
 * I’m using a php variable elsewhere to pull up related links, and it works fine
   in the wp_list_bookmarks statement, but for some reason its not working in wp_query,
   any ideas why?

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

 *  Thread Starter [thejester12](https://wordpress.org/support/users/thejester12/)
 * (@thejester12)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-a-variable-inside-wp_query/#post-920321)
 * Is this possible? Seems like it should be simple?
 *  Thread Starter [thejester12](https://wordpress.org/support/users/thejester12/)
 * (@thejester12)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-a-variable-inside-wp_query/#post-920355)
 * Does it have something to do with global variables and whatnot?
 *  [renato_s](https://wordpress.org/support/users/renato_s/)
 * (@renato_s)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-a-variable-inside-wp_query/#post-920367)
 * Try to use double quotes instead of single ones to wrap the query parameters.
 *  Thread Starter [thejester12](https://wordpress.org/support/users/thejester12/)
 * (@thejester12)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-a-variable-inside-wp_query/#post-920371)
 * Wow, so simple… That’s why as a designer I get frustrated with PHP/Programming
   stuff sometimes… Syntax gets me every time!
 * Thanks so much! It worked!

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

The topic ‘Using a variable inside WP_Query’ is closed to new replies.

## Tags

 * [tags](https://wordpress.org/support/topic-tag/tags/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [thejester12](https://wordpress.org/support/users/thejester12/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/using-a-variable-inside-wp_query/#post-920371)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
