WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Using variable passed in URL

[Resolved] 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)
  • If you’re trying to implement a custom term to search on, I would take a look at the Custom Queries Codex page. You’ll need to define your custom terms, possibly add some rewrite rules depending on whether or not you use permalinks.

    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

    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 ‘[Resolved] Using variable passed in URL’ is closed to new replies.