Home › Forums › General Issues › Search Relational Items by their ACF Fields › Reply To: Search Relational Items by their ACF Fields
I just realized that what I did will cause to only search the field when that’s not really what we want. It would need to be more like this
add_filter('acf/fields/relationship/query/name=YOUR_FIELD_NAME', 'add_artist_meta_query', 10, 3);
function add_artist_meta_query($args, $field, $post_id) {
if (isset($args['s'])) {
$args['meta_query] = array(
'relation' => 'OR'
array(
'key' => 'YOUR_FIELD_NAME',
'value' => $args['s'],
'compare' => 'LIKE'
)
array(
'key' => 'YOUR_FIELD_NAME',
'value' => $args['s'],
'compare' => 'NOT LIKE'
)
);
}
return $args;
}
I think that’s the only way to accomplish post searching the title and the meta field and returning results for each. I may also be missing what’s needed for searching the title. You may need to alter that as well, but I can’t be sure. Probably a lot more complicated that I originally thought and would take quite a bit of testing to get it right.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.