WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using variable passed in URL (4 posts)

  1. circleatseven
    Member
    Posted 2 years ago #

    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

  2. eryanv
    Member
    Posted 2 years ago #

    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.

  3. circleatseven
    Member
    Posted 2 years ago #

    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

  4. circleatseven
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic