Home › Forums › Backend Issues (wp-admin) › Relationship Search
Hi,
I would like to expand the fields that are searchable in the ACF Relationship field. Based upon documentation, this looks possible by enhancing the Relationship Query. Scenario:
I have an ACF field (Text) called “_slug” attached to posts.
I have an ACF field (Relationship) attached to a page.
When searching the Relationship field (in the Worpdress admin), I would like the custom text field (“_slug”) to be included in the search query.
Using this as my guide…
http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
…here is where I am at:
function my_relationship_query_add_slug( $args, $field, $object ) {
//gets the slug
$slug = get_field('_slug', $object->ID);
//add slug to WP_Query
$args['meta_value'] = $slug;
}
add_filter('acf/fields/relationship/query', 'my_relationship_query_add_slug', 10, 3);
That does not work. I suspect it’s actually overwriting the WP_Query rather than adding to it. Or maybe the meta query isn’t properly formatted. Or… something.
Help?
The first thing I notice is that you’re not returning the new $args.
You probably also need to add the ‘meta_key’ to search
function my_relationship_query_add_slug( $args, $field, $object ) {
//gets the slug
$slug = get_field('_slug', $object->ID);
// add meta_key to WP_Query
$args['meta_key'] = 'meta_key to search for _slug in';
//add slug to WP_Query
$args['meta_value'] = $slug;
// return new $args
return $args;
}
add_filter('acf/fields/relationship/query', 'my_relationship_query_add_slug', 10, 3);
Oh thanks, yeah, I need the return. But this still doesn’t quite solve it…
You added a meta_key, but that’s what the user is searching for. (Again, this in the admin. The user is trying to add a post in ACF Relationship by searching for a _slug.)
Oh wait, scratch that. I had my meta_key and meta_value switched around. This gets me very close to a solution:
function my_relationship_query_add_slug( $args, $field, $object ) {
$slug = get_field('tvi_slug', $object->ID);
$args['meta_key'] = $slug;
$args['meta_value'] = 'THING HERE';
return $args;
}
add_filter('acf/fields/relationship/query', 'my_relationship_query_add_slug', 10, 3);
But “THING HERE” is what the user is entering (searching for). How do I capture that?
See this page http://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/, the select2_args filter at the bottom. I think this is what you need to use to get a value and add it to the arguments sent during the ajax call. Beyond that I can’t help you, I don’t have any experience altering the select2 calls.
You may want to start a new topic and ask specifically about how to set up javascript to get the selection of one field and send it with the select2 ajax call and use the value to alter the search args. Someone else may be able to help you with that.
The topic ‘Relationship Search’ is closed to new replies.
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.