search using multiple taxonomies and keyword

I have made six custom taxonomies. I want to perform search using keyword and custom taxonomy in wordpress
For eg: I want to have a dropdown with only four custom taxonomies(not all 6) and then filter the search based on the keyword and the custom taxonoy selected by me.
In searchform.php, I pasted the code

<form method="get"  class="search-form" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <fieldset>
        <label for="search">Search:</label>
        <select id="search" class="select" title="Whole Site" name="cat">
                <option value="news">News Articles</option>
                <option value="publications">Publications</option>
                <option value="professionals">Professionals</option>
                <option value="company">Company</option>
        </select>
        <div class="input-holder">
            <input type="text" placeholder="Enter keyword..." class="text" name="s" id="s">  
            <button type="submit" name="submit" class="submit">Search</button>
        </div>
    </fieldset>
</form>

then in search.php , I retrieved the select value as $category_index=$_REQUEST['cat'];
$ss=$_REQUEST['s'];

Further, I do not know how to filter.

Answer

I would recommend you to use a hook for pre_get_posts action. This action is fired before appropriate posts are fetched from db. In this hook you can add extra parameters to your query and have influence on returned results. So the basic implementation of your hook could be like this:

add_action( 'pre_get_posts', 'wpse8170_pre_get_posts' );
function wpse8170_pre_get_posts( $query ) {
    if ( is_search() ) { // check if it is search page
        $query->set( 'arg', 'value' ); // add additional arguments to the query
    }
}

Pay attention that arg and value parameters are generic and was mentioned as example. You have to use correct arg and value for your query. All list of available arguments for the query you can read in codex.

Attribution
Source : Link , Question Author : user930026 , Answer Author : Eugene Manuilov

Leave a Comment