I have a custom post type called “servizio” which can have a list of “doctor” (cpt) associated. These doctors are linked to the “servizio” using the acf “medici”, infact if I inspect the table postmeta I will get this situation:
as you can see the “servizio” meta_key has “medici” which contains a list of references of the linked “doctor” (cpt)
So I wrote this code in the functions.php to extend the wp search functionalities:
function add_acf_in_search($query)
{
if ($query->is_search() && $query->is_main_query()) {
$s = $query->query_vars['s'];
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'medici',
'value' => '"' . $s . '"',
'compare' => 'LIKE'
),
array(
'key' => 'medici',
'value' => '%:' . $s . ';%',
'compare' => 'LIKE'
)
);
$query->set('meta_query', $meta_query);
}
}
add_action('pre_get_posts', 'add_acf_in_search');
This code is supposed to return the “servizio” if I search for a “doctor” associated as acf repatable field, but it doesn’t.
Example:
servizio: "lorem ipsum"
linked doctors "mario rossi" "john travolta"
If I search for “travolta” I should get the servizio “lorem ipsum”.
