WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Creating a search query (5 posts)

  1. AidanG
    Member
    Posted 6 months ago #

    [Moved to Hacks forum]
    Hello, I'm using a search mod called Advanced Search (read more on it here: http://wpadvancedsearch.com/) 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:

    <?php
    /*
    Template Name: Search Page
    */
    
    session_start();
    
    $args = array();
    $args['form'] = array(
                        'action' => 'http://adgo.ca/uoftclubs/search-results/');
    $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; ?>
        </div>

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

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

    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' => 'http://adgo.ca/uoftclubs/search-results/');

    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.

  2. shandiz
    Member
    Posted 6 months ago #

    what are you using session? here is an example about how to set form, fields, and its action. wpadvancedsearch.com/docs/form-arguments

  3. AidanG
    Member
    Posted 6 months ago #

    Thanks so much for your response.

    So I should have this on my first page:

    $args['form'] = array(
                        'action' => 'http://adgo.ca/uoftclubs/search-results/',
                        '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

  4. AidanG
    Member
    Posted 6 months ago #

    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.

  5. AidanG
    Member
    Posted 6 months ago #

    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.

Reply

You must log in to post.

About this Topic