I use the Search & Filter plugin to enable users search & also filter our products. Problem now is it doesn't include results for related product acf meta keys/values. For example if a user searches for "artsy", it shows "Nothing found" but there are products that have the meta value of "Artsy" and I'd want the search to return those products. Note: The plugin uses "_sf_s" as the query parameter.
This is what I've been trying;
function filter_search_by_acf_meta_keys($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
// Check if the Search & Filter Pro plugin is active
if (isset($_GET['_sf_s']) && !empty($_GET['_sf_s'])) {
$search_query = sanitize_text_field($_GET['_sf_s']);
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'keywords_tags',
'value' => $search_query,
'compare' => 'LIKE'
),
array(
'key' => 'region_field',
'value' => $search_query,
'compare' => 'LIKE'
)
// Add more ACF meta keys as needed
);
$query->set('meta_query', $meta_query);
$query->set('s', $search_query);
}
$query->set('post_type', array('products', 'listings'));
}
add_action('pre_get_posts', 'filter_search_by_acf_meta_keys');
It looks like the query looks for products or listings that have title/content that matches the search query AND meta data that matches the search query. Try removing the
$query->set( 's', $search_query )
line and see if you get results you expect.If you receive expected results, then you'll need two separate
WP_Query
instances, and combine the results. Similar situation here: https://wordpress.stackexchange.com/questions/416429/how-to-make-or-condition-in-wp-query/416442#416442