Skip to main content

So it'sIt'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) ButShow 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_refersearch_refer to the refine drop down, store the original results array in a variable, and do a conditional so that only if the search_refersearch_refer was NOT refinerefined, 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. JustJust 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_refersearch_refer was refinerefined and it worked perfectly.

But nowNow I'm wondering if storing the array somehow is the best way to do this anyhow? 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)'page =>'Articles (10)', posts =>'Blog Posts (5)'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>';
}

So 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 refine, 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 refine and it worked perfectly.

But now I'm wondering if storing the array somehow is the best way to do this anyhow? 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>';
}

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>';
Source Link

Temporarily storing main search result

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.

So 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 refine, 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 refine and it worked perfectly.

But now I'm wondering if storing the array somehow is the best way to do this anyhow? 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>';
}