Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to let users have a filter option when they submit a search.

My search form:

<form class="searchform" method="get" action="http://www.example.com/">
            <input class="search" name="s" type="text" value="Search Here"/>
            <select name="search_type" id="cat" class="postform" style="display: none; ">
                <option value="all" selected="selected">both</option>
                <option class="level-0" value="filterone">filterone</option>
                <option class="level-0" value="filtertwo">filtertwo</option>
            </select>
            <button value="submit" class="searchBtn"><span class="hidden">Submit</span></button>
</form>

The problem im having is that the optoin paremeters im passing through the url are removed. for example when I go to:

http://www.example.com/?s=apple&search_type=filterone

I am imediatley returned to:

http://www.example.com/?s=apple

Ive tried many different things to make this work, but it still doesn't work:

add_filter('query_vars', 'search_vars' );
function search_vars( $qvars )
{
$qvars[] = 'search_type';
return $qvars;
}
function my_search_filter( $query )
 {
      if( is_search() )
      {
                $query->set( 'search_type', 'schools' );
      }
 }
add_filter( 'pre_get_posts', 'my_search_filter' );

When I go to any other page besides the home page, the url parameters work fine. Also I am using custom permalinks. I hope this is enough information.

So my question is, how can I stop the parameter variables from being removed?

share|improve this question
I think you need add_query_arg..‌​. – soulseekah Mar 24 '12 at 15:52
I think add_query_arg() just returns a string of the url I want, and doesn't actually add parameters to the real url. – Steven Baltay Mar 24 '12 at 22:07
Sorry, meant add_query_var actually :) thanks for pointing that out. – soulseekah Mar 25 '12 at 3:16

closed as too localized by toscho Feb 17 at 22:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

3 Answers

In the above code on this line

<input class="search" name="s" type="text" value="Search Here"/>

change name of text input from "s" to "s2" or whatever you want so that the page will not redirect to search page and your passed parameters will not remove.

After getting the value of your "search_type" parameter redirect page to search page like this.

if(is_search()){
 wp_redirect( home_url()."?s2=".$_GET['s2'] );
 exit;

}

share|improve this answer
I think im having bigger problems somewhere, because I cant use php get and post on a page, it just removes the parameters. the only way I can pass parameters is through the s variable in a query, or through a direct link to a page ending in .php. – Steven Baltay Mar 24 '12 at 21:15

I have discovered the the wordpress SEO plugin somehow interferes with url parameters. I disabled the plugin and the url parameters seem to stick just fine.

share|improve this answer

If I'm understanding this correctly, you simply need this inside your <form> tag:

<input type="hidden" name="search_type" value="filterone" /> 
share|improve this answer

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