Home › Forums › Backend Issues (wp-admin) › Relationship field not searching custom fields
I have a relationship field that is filtered to only query the “session” custom post type. For that post type I have a “name” custom field, along with many others, and the default title field.
Many of the posts will have long titles, so I would like to query by the “name” field too, to find the correct session.
I have already added code to show the “name” after the title in the query results:
function session_relationship_result($html, $post){
$tID = $post->ID;
$tName = $post->post_title;
//SESSION SPEAKER
$sessionSpeaker = get_field('name', $tID);
return $html . '<span style="margin-left: 5px; color: #cccccc;">('.$sessionSpeaker.')</span>';
}
add_filter( 'acf/fields/relationship/result/name=feature_speakers', 'session_relationship_result', 10, 2);
When I query to find a post in the relationship field, it shows “no results” when I query by the name. It seems to only query the title field.
Is it possible add this additional functionality?
Hey Rob,
Did you ever figure this out, I’m looking for a similar functionality, except I just want to be able to search by the Post ID as well.
I am also interested in knowing if a solution was ever found for this.
This is why I ALWAYS edit my questions whenever I find an answer. 🙂
The search in the relationship fields uses the standard WP ‘s’ parameter for WP_Query http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter. If you want to extend the query to also search custom fields then you’ll need to modify the query.
I would start here https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
And I’d take a look at at the parse_search()
function in /wp-includes/query.php. It’s on line 2112 in the current version of WP.
I’m not sure you can do this by just adding a complicated meta query. You may need to hook into where the query is done and alter it there, you may even need to build a completely custom query and to the db query yourself.
There are some plugins available that will alter searches, one of them is https://wordpress.org/plugins/search-everything/, but I’ve seen this plugin cause a site with a lot of posts and custom fields to time out.
You must be logged in to reply to this topic.
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.