I have a real estate website.
I have the filters set up to enable a granular search utilising ACF fields.
My issue is when a client puts in a reference number for a particular property number it always brings up 2 or 3 properties (always one of them is the correct property).
How do I ensure that it only pulls in the single property that is requested.
URL: https://dreamhomesalmeria.com
Hi @TheGuru
The Filter - Search element currently is only performing standard WordPress search (“s” parameter to the target query).
It wouldn’t search for the content inside your meta field. It’s only search from post_title, post_content and post_excerpt.
Regards,
Jenn
function custom_search_query($query) {
if ($query->is_search() && !is_admin()) {
$query->set(‘post_type’, ‘property’); // Replace property with your CPT slug
$search_term = $query->get('s');
$query->set('s', '');
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'property_name', // Replace with your 1st meta_key
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'reference_number', // Replace with your 2nd meta_key
'value' => $search_term,
'compare' => 'LIKE'
)
);
$query->set('meta_query', $meta_query);
}
}
add_action(‘pre_get_posts’, ‘custom_search_query’);