I’ve tried to solve this with ChatGPT, but no success.
This is code that works only with the meta field. When I input the name of the product search is empty and when “podnaslov” is entered search result is as should be.
function custom_search_filter($query) {
if (!is_admin() && $query->is_main_query()) {
if (is_search()) {
// Limit the search to WooCommerce products
$query->set('post_type', 'product');
// Modify the search query to search in "title" and "podnaslov" fields only
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'podnaslov', // Change to the actual custom field name
'value' => get_search_query(),
'compare' => 'LIKE',
),
);
$query->set('meta_query', $meta_query);
}
}
}
add_action('pre_get_posts', 'custom_search_filter');
Any help is appreciated. TIA.
Best regards,
Goran