Title: PHP Variable in WP_Query
Last modified: August 19, 2016

---

# PHP Variable in WP_Query

 *  [joshuaonesix](https://wordpress.org/support/users/joshuaonesix/)
 * (@joshuaonesix)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-variable-in-wp_query/)
 * The blog im working on will be highlighting some items. For every item there 
   will be 3 posts (Story, Blog, Press Release) I want to display them on one page
   based on the story. The 3 posts will have a unique tag only unique to that item.
   Ive figured out how to generate that tag based on pulling it from the story. 
   Now i need to get it added to the other queries. The code below works with the
   tag hard coded in:
 *     ```
       $my_query = new WP_Query('cat=10&tag=uniquetag');
       	while ($my_query->have_posts()) : $my_query->the_post();
       	the_content();
       	endwhile;
       ```
   
 * but not when used with the php variable
 *     ```
       $my_query = new WP_Query('cat=10&tag='.$myvariable);
       	while ($my_query->have_posts()) : $my_query->the_post();
       	the_content();
       	endwhile;
       ```
   

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-variable-in-wp_query/#post-1443688)
 * Where/how are you setting up $myvariable?
 *  Thread Starter [joshuaonesix](https://wordpress.org/support/users/joshuaonesix/)
 * (@joshuaonesix)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-variable-in-wp_query/#post-1443761)
 * this is before the end of the first loop.
 *     ```
       <?php
       $posttags = get_the_tags();
       $count=0;
       	if ($posttags) {
       	foreach($posttags as $tag) {
       		$count++;
       	if (1 == $count) {
   
       		$mytag = $tag->name . ' ';
   
       		echo $mytag;
       					}
       								}
       					}
   
       ?>
       ```
   

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

The topic ‘PHP Variable in WP_Query’ is closed to new replies.

## Tags

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

 * 2 replies
 * 2 participants
 * Last reply from: [joshuaonesix](https://wordpress.org/support/users/joshuaonesix/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/php-variable-in-wp_query/#post-1443761)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
