Documentation - Actions / Filters - Filter – apto/query_filter_valid_data

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Filter – apto/query_filter_valid_data

Name: apto/query_filter_valid_data
Type: Filter
Arguments: $query

The custom sorting is implemented on the front end based on the provided query arguments. When these arguments align, the plugin engine automatically adjusts the query to deliver objects in the specified custom order. However, when the query contains differing arguments, a sorting match may not occur. That is often attributed to excluded terms in the query. This discrepancy causes a mismatch between the query and the applied custom order, disrupting the intended sorting.
By employing the following code, these excluded terms can be disregarded, facilitating a match with the established sorting configuration.
The $check_on_taxonomy should include the taxonomy name for which the code will check for.


    add_filter('apto/query_filter_valid_data', 'custom_woocommerce_query_filter_valid_data', 99);
    function custom_woocommerce_query_filter_valid_data( $reference_query )
        {
            
            $check_on_taxonomy  =   'series';
                
            $tax_data   =   $reference_query->tax_query->queries;
            
            if(isset($tax_data['relation']))
                unset($tax_data['relation']);
            
                            
            //unest all other taxonomies
            foreach ( $reference_query->tax_query->queries    as  $key    =>  $item_tax_data )
                {                        
                    if ( !isset ( $item_tax_data['taxonomy'] ) )
                    return $reference_query;
                    
                    if ( $item_tax_data['taxonomy']  !=  $check_on_taxonomy )
                        continue;
                    
                    if ( isset ( $item_tax_data['operator'] )   &&  $item_tax_data['operator']  =  'NOT IN' )
                        unset ( $reference_query->tax_query->queries[$key] );
                    
                }
            
            return $reference_query;
            
        }

The code should be placed inside a php file on wp-content/mu-plugins folder or theme functions.php.