Dear Support Team,
I hope this message finds you well. I am currently experiencing difficulties trying to apply changes to the product search on the frontend of my store using your theme.
My goal is to filter and sort search results based on specific categories. I have attempted to implement the following code in my functions.php file:
function mi_filtro_de_busqueda($query) {
// Verificamos si estamos en el frontend y en la búsqueda de productos
if (!is_admin() && $query->is_search() && $query->get(‘post_type’) === ‘product’) {
// Obtenemos las categorías que queremos priorizar
$categories = array(‘para-leches-vegetales’, ‘licuadoras’, ‘extractores-de-jugo’, ‘para-harinas-y-pan’, ‘filtros-de-agua’, ‘para-el-jardin’, ‘repuestos’, ‘utensilios-de-hogar-y-cocina’, ‘deshidratador’);
// Filtramos los resultados de la búsqueda
$query->set(‘tax_query’, array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => $categories,
),
));
// Ordenamos los productos por categorías y precio
$query->set(‘orderby’, array(‘taxonomy’ => ‘ASC’, ‘meta_value_num’ => ‘DESC’));
}
}
// Añadimos el filtro a la acción ‘pre_get_posts’
add_action(‘pre_get_posts’, ‘mi_filtro_de_busqueda’);
However, it seems that the changes are not being applied correctly. Could you please review the code and provide guidance on the best way to customize product search on the frontend using your theme?
I appreciate your assistance and prompt response.