Home › Forums › General Issues › Search in the relationship field with text › Reply To: Search in the relationship field with text
You are trying do a search and return posts that have a related post that has a specific title. This will not work. It would be like searching for posts that have a related post where the related post has a specific meta value.
The only way that you can do this is to make the information you want to search for part of the post that your searching for it in. This will require that you create another custom field that can hold the title of the related post.
add_filter('acf/update_value/name=relationship_field', 'my_add_titles_to_post_for_search', 10, 3);
function my_add_titles_to_post_for_search($value, $post_id, $field)( {
// use a new field, it does not need to be an acf field
// first delete anything it might hold
delete_post_meta($post_id, '_hidden_relationship_title');
if (!empty($value)) {
$posts = $value;
if (!is_array($posts)) {
$posts = array($posts);
}
foreach ($posts as $post) {
// add each related post's title
add_post_meta($post_id, '_hidden_relationship_title', get_the_title($post), false);
}
}
return $value;
}
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.