Search fix. Hide hidden products when searching.

add_action( 'pre_get_posts', 'search_query_fix' );
function search_query_fix( $query = false ) {
    if(!is_admin() && is_search()){
        $query->set( 'tax_query', array(
            array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'exclude-from-search',
                'operator' => 'NOT IN',
            )
        ));
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *