My filter stopped working

Tagged:
  • estevaoacioli
    Participant
    5 years, 1 month ago #11080

    Before I used the pre_get_posts function to filter search results, now it does not work anymore

    function search_filter_tax_query($query) {
        //if ( $query->is_search ){
    	if ( $query->is_main_query() && is_search() ) {
    	
    	if(isset($_GET['stdc'])) { $tdc = $_GET['stdc']; } else { $tdc = ''; }
    	if(isset($_GET['sorg'])) { $org = $_GET['sorg']; } else { $org = ''; }  
    	if(isset($_GET['sbase'])) { $base = $_GET['sbase']; } else { $base = ''; }	
    	if(  ( $tdc != '' ) || ( $org != '' ) || ( $base != '')  ) {
    		
    		$sub_args = array();				
    		
    		if( $tdc != '' ){ 
    					$sub_args[0] =	array(
                            'taxonomy' => 'content_types',
                            'field' => 'term_id',						
                            'terms' => $tdc
                        );
    				
    				}
    		if( $org != '' ){ 
    					$sub_args[1] =	array(
                            'taxonomy' => 'content_origem',
                            'field' => 'term_id',
                            'terms' => $org
                        );
    				
    				}
    		if( $base != '' ){ 
    					$sub_args[2] =	array(
                            'taxonomy' => 'content_base',
                            'field' => 'term_id',
                            'terms' => $base
                        );
    				
    				}
    	} 
          
          $query->set( 'tax_query', $sub_args );
        }
        return $query;
    }
    add_action( 'pre_get_posts', 'search_filter_tax_query' );
    estevaoacioli
    Participant
    5 years, 1 month ago #11081
    add_filter('query_vars', 'introduce_qvs');
    function introduce_qvs($qv) {
        $qv[] = 'sorg';
    	$qv[] = 'stdc';	
    	$qv[] = 'sbase';
        $qv[] = 'sdti';
    	$qv[] = 'sdtf';
        return $qv;
    }
    wpsolr
    Keymaster
    5 years, 1 month ago #11084

    You have several alternatives to coding to do just that.

    1) Use Toolset
    Build you search page with an archive View, add your 3 filters to the view. WPSOLR will automatically convert the View query to an Elasticsearch query.
    Here is the documentation: https://www.wpsolr.com/guide/configuration-step-by-step-schematic/activate-extensions/toolset-views-add-on/

    2) Preselect facet values
    With WPSOLR, you can filter by a facet value, like your taxonomy terms.
    Here is a discussion on how to filter results with in-stock product taxonomy: https://www.wpsolr.com/forums/topic/products-out-of-stock-gaps-in-results/

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.