Support » Plugins » Hacks » Creating a search query

  • Resolved AidanG


    [Moved to Hacks forum]
    Hello, I’m using a search mod called Advanced Search (read more on it here: and essentially what I want to do is push my search from one page – the one that the user inputs their query – to a results page that takes the query and displays the results. Below I will post my php from each page.

    For searchpage.php:
    Here is the php:

    Template Name: Search Page
    $args = array();
    $args['form'] = array(
                        'action' => '');
    $args['wp_query'] = array(
                            'post_type' => 'post',
                            'order' => title);
    $args['fields'][] = array(
                            'type' => 'meta_key',
                            'format' => 'checkbox',
                            'label' => 'Pick Your Campus:',
                            'compare' => 'IN',
                            'meta_key' => 'campus',
                            'values' => array('utsg' => 'UTSG', 'utsc' => 'UTSC', 'utm' => 'UTM'));
    $args['fields'][] = array(
                            'type' => 'search',
                            'placeholder' => 'Search for a club!');
    $args['fields'][] = array(
                            'type' => 'submit');
    $my_search_object = new WP_Advanced_Search($args);

    and the relevant html:

    <div class="main-search">
            <?php $my_search_object -> the_form(); $_SESSION['searchquery'] = $my_search_object; ?>

    and then for the search-results.php:
    the php and html:

    <div id="search-results">
            $my_search = $_SESSION['searchquery'];
            $temp_query = $wp_query;
            $wp_query = $my_search->query();
            if ( have_posts() ):
                while ( have_posts() ): the_post(); ?>
                        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                        if ($post->post_type == 'gs_field') the_field('description');
                        else the_excerpt();
            else :
                echo '<p>Sorry, no clubs matched what you searched.</p>';
            $wp_query = $temp_query;

    I have it set up so I created two pages in wordpress, one for each of these templates. You can see in the first page that I’m using the

    $args['form'] = array(
                        'action' => '');

    to submit the form to my results page. I’m also using “$_SESSION” variables to carry “$my_search_object” to the next page.

    The issue is, when I make the query and it redirects to my results page, the query itself doesn’t carry over (i.e. no matter what I search every single post will appear on the results page). When I have the results page code on the same one as the query page, the search function works perfectly.

    There is no doubt in my mind that there are some very poor errors made here. I’m learning so I’d appreciate being taught as you correct my mistakes.

    Let me know if you need anything to help you figure out what’s going wrong.

Viewing 4 replies - 1 through 4 (of 4 total)
  • what are you using session? here is an example about how to set form, fields, and its action.

    Thanks so much for your response.

    So I should have this on my first page:

    $args['form'] = array(
                        'action' => '',
                        'method' => 'GET',
                        'id' => 'search-variable',
                        'name' => 'my_search_object',
                        'class' => '_search-class');

    and on my results page I can call the query using:

    $my_search_object = $_GET['my_search_object'];
    $my_search = $my_search_object;
    $temp_query = $wp_query;
    $wp_query = $my_search->query();

    And that should display my query? Did I phrase my $_GET() statement correctly? I “get” the “name” variable correct?

    Thanks for your help

    Okay so, doing the previous resulted in this error being displayed: “Fatal error: Call to a member function query() on a non-object in /home/content/96/8892496/html/uoftclubs/wp-content/themes/twentyfourteen/search-results.php on line 17”

    So my $_GET was obviously done improperly because it isn’t “getting” the query variable I wanted it to.

    I got it! Just tinkered around and realize is was passing the $args not the query, so then I just ran a query on the variable and it worked like a charm.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating a search query’ is closed to new replies.