Title: Using variable passed in URL
Last modified: August 20, 2016

---

# Using variable passed in URL

 *  Resolved [circleatseven](https://wordpress.org/support/users/circleatseven/)
 * (@circleatseven)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/using-variable-passed-in-url/)
 * I’m able to pass a variable in a url like so:
    `http://mysite.com/?searchterm
   =544`
 * .. and I am able to GET the variable and display it in a php echo like so (this
   is placed before the loop):
 *     ```
       <?php $searchterm = $_GET["searchterm"]; ?>
       Search term: <?php echo $searchterm; ?>
       ```
   
 * BUT if I insert the echo in the query_post like so:
 *     ```
       <?php query_posts('p=<?php echo $searchterm; ?>');
       	if ( have_posts() ) : while ( have_posts() ) : the_post();
       ?>
       ```
   
 * … it doesn’t seem to work correctly, but if I manually enter 544 in place of 
   the echo it works as expected.
 * Any ideas!? Thanks

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

 *  [eryanv](https://wordpress.org/support/users/eryanv/)
 * (@eryanv)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/using-variable-passed-in-url/#post-2264900)
 * If you’re trying to implement a custom term to search on, I would take a look
   at the [Custom Queries Codex](http://codex.wordpress.org/Custom_Queries) page.
   You’ll need to define your custom terms, possibly add some rewrite rules depending
   on whether or not you use permalinks.
 *  Thread Starter [circleatseven](https://wordpress.org/support/users/circleatseven/)
 * (@circleatseven)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/using-variable-passed-in-url/#post-2264909)
 * Thanks, but that’s not really what I’m trying to do. I should’ve used a different
   variable name instead of “$searchterm”… sorry if that made it confusing.
 * All I really need to do is simply insert that variable from the url (‘544’) into
   the query_post so I can display a specific post (id of ‘544’) in a page… it would
   look like this:
 *     ```
       <?php query_posts('p=544');
       	if ( have_posts() ) : while ( have_posts() ) : the_post();
       ?>
       ```
   
 * thanks again
 *  Thread Starter [circleatseven](https://wordpress.org/support/users/circleatseven/)
 * (@circleatseven)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/using-variable-passed-in-url/#post-2264922)
 * Figured it out – just changed the single quotations to double and it worked…
 * Changed:
 *     ```
       <?php query_posts( 'p=<?php echo $searchterm; ?>' );
       	if ( have_posts() ) : while ( have_posts() ) : the_post();
       ?>
       ```
   
 * To:
 *     ```
       <?php query_posts( "p=<?php echo $searchterm; ?>" );
       	if ( have_posts() ) : while ( have_posts() ) : the_post();
       ?>
       ```
   
 * Ugh.

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

The topic ‘Using variable passed in URL’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [circleatseven](https://wordpress.org/support/users/circleatseven/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/using-variable-passed-in-url/#post-2264922)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
