1

I have several different post types and when someone searches, it lumps all the post types together and then I have a drop down select for the user to refine their search by the different post types if they want to. I decided to generate the drop down automatically by post count so only sections that have any results will be added. I've also got each option displaying the total number of results in that section so I end up with an array that has the post name, the display name and the total amount of results and then I use that array to build the drop down menu and display selected, etc.

It's all working the way I wanted, except that I'm having one issue - when I click one of the select options to refine the search, the new query overwrites the array and so I lose the counts and other select items.

I'd like the drop down to keep the data from the original query instead of the refined query. For example, let's say my total search results are 50 - then the drop down shows: Show all(50) Pages(20) Blog Posts(15) Glossary Terms(10) Reviews (5), but if someone clicks on Blog Posts, the other sections just disappear.

What I originally thought I'd do is to add a search_refer to the refine drop down, store the original results array in a variable, and do a conditional so that only if the search_refer was NOT refined, then build the drop down, otherwise use the array stored in the variable.

Which was a great idea I thought, except it doesn't work - I tried storing the array in a global variable and as soon as the page refreshes with the refined search results, the array shows NULL. Just to test if what I had in mind would work, I stored the original array in a transient and then called the transient if the search_refer was refined and it worked perfectly.

Now I'm wondering if storing the array somehow is the best way to do this? Maybe if the form to submit the refined search used a different query? I know I'm probably making this more complicated than it actually is and there's probably a really easy way to do what I want it to do.

In case it matters, I've included the code for the drop down below - the array is just a key value pair and I check if a post type has at least one item, if it does, I concatenate the count to the name and so it's like: page =>'Articles (10)', posts =>'Blog Posts (5)', etc.

echo '<form id="types" class="select" method="get" action="'.$bloginfo.'">
    <label class="" for="s">'.$label.'</label><input type="hidden"   
    value="'.$searchquery.'" name="s" id="s" /><select id="post_types" 
    name="post_types" onchange="this.form.submit()">';

foreach ( $my_array as $key => $val ) {
    if ( $picked === $key  || ( $search_refer == 'gloss' && $key =='glossary' ) ) {
        echo '<option value="' . $key . '" selected name="post_type">' . $val . '</option>';
    } else {
        echo '<option value="' . $key . '" name="post_type">' . $val . '</option>';
    }
}

echo '</select><input name="site_section" type="hidden" value="refine" />
    <noscript><input type="submit" id="searchsubmit" value="Go" /></noscript> 
    </form>';
1

The way I see it, you have three options:

  1. Store the global search data in $_SESSION
  2. Store the global data in a transient
  3. Run the query twice, with & without the post type filter

I tried storing the array in a global variable and as soon as the page refreshes with the refined search results, the array shows NULL.

Of course. PHP has no "memory" of the previous request, that's the curse of HTTP.

Personally, I would opt for option 1 or 2. 3 is okay but more intensive on the server - it all depends on the level of traffic and how many concurrent searches you expect to handle.

  • Thank you, I actually did use a transient and it worked perfectly, but I was testing on a development server so I'm the only one searching. The transient is stored just by name, so wouldn't that get overwritten if someone else does a search? Adding a random number wouldn't work. OH wait a minute...if I added the search term variable to the name of the transient while storing it...? Let me see if that works! – CindyH May 30 '15 at 2:51
  • Yes that's got it! I'll set them to delete in just a few minutes and add in a 'your search expired' or something if the transient can't be found. Thanks for the input! – CindyH May 30 '15 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.