[Resolved] Creating a search query
[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.
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.
- The topic ‘[Resolved] Creating a search query’ is closed to new replies.