Plugin to apply search filtering in wordpress

I am a student learning wordpress these days while working with wordpress i though to implement the search filters on my website by default wordpress searches the posts i want to apply filters on search. Let me explain in simple words what i want to do. I have a number of products in my database table a user can search a product by its unique id or by its name. When a user will search select drop down this will show him following options to search with unique id(Brand ID). He may search a product that is being start with specific entered word. I am worried how i can alter the search query of wordpress. If anyone can help me in doing this task? I will highly appreciate.

Related posts

Leave a Reply

1 comment

  1. I hope this code helps you a bit to start.

    function ba_SearchFilter($query) {
        if (!$query->is_search) {
            return $query;
        }
        if (isset($_POST['cat'])){
            $query->set('category__and', $_POST['cat']);
        }
        if (isset($_POST['tags'])){
            $query->set('tag__and', $_POST['tags']);
        }
        return $query;
    }
    //hook filters to search
    add_filter('pre_get_posts','ba_SearchFilter');
    
    function ba_search_with_filters(){
        $out = '<form role="search" method="get" id="searchform" action="'. home_url( '/' ).'">
        <div><label class="screen-reader-text" for="s">Search for:</label>
            <input type="text" value="" name="s" id="s" /><br />';
                $categories=  get_categories(); 
                foreach ($categories as $category) {
                    $option = '';
                    $option .= '<input type="checkbox" name="cat[]" id="cat[]" value="'.$category->term_id.'"> ';
                    $option .= $category->cat_name .'<br />';
                    $out.= $option;
                }
                $tags=  get_categories(); 
                foreach ($tags as $tag) {
                    $option = '';
                    $option .= '<input type="checkbox" name="tags[]" id="tags[]" value="'.$tag->term_id.'"> ';
                    $option .= $tag->cat_name .'<br />';
                    $out.= $option;
                }
        $out .='<input type="submit" id="searchsubmit" value="Search" />
            </div>
            </form>';
        return $out;
    }
    
    add_shortcode('search_with_filter','ba_search_with_filters');
    
    ?>
    

    You have to change the code as your requirement. Also you can see the plugin http://wordpress.org/extend/plugins/advance-wp-query-search-filter/