Support

Account

Home Forums Backend Issues (wp-admin) Relationship field not searching custom fields

Solving

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 a solution for this.

  • 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.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Relationship field not searching custom fields’ is closed to new replies.