Home › Forums › General Issues › ACF Relationship Field Search Filtering – Also look for taxonomies
I’ve setup a relationship field which I’ve tweaked to show a couple of extra fields like this:
add_filter('acf/fields/relationship/result/name=featured_projects', __NAMESPACE__ . '\\showreel_relationship', 10, 4);
function showreel_relationship( $title, $post, $field, $post_id ) {
$director_id = get_post_meta ( $post->ID, 'director', true );
$director = get_post($director_id);
$director_name = $director->post_title;
$client = get_field( 'client', $post->ID );
$client_name = $client->name;
$title = $director_name .' – '. $title . ' – ' . $client_name;
// return
return $title;
}
Currently I can type something into search and it will look for either project name or director name, but if I write something that ties into taxonomies it won’t find anything.
Searching on director name
Searching on project name
Searching on client name
How can I modify the query so that it looks for project name, director name and client name (which is a taxonomy)?
I am aware that I can setup something like this (not tested):
add_filter('acf/fields/relationship/query/name=featured_projects', __NAMESPACE__ . '\\project_relationship_query', 10, 3);
function project_relationship_query( $args, $field, $post_id ) {
$args['post_status'] = 'publish';
$args['meta_query'] = array(
array(
'key' => my_key,
'value' => $args['s'],
'compare' => 'LIKE',
)
);
return $args;
}
But then it will only look for posts with that key. I want to modify the current query so that it looks for taxonomies on top of project and director name.
According to this site for $args to use https://www.billerickson.net/code/wp_query-arguments/
'meta_query' => array( //(array) - Custom field parameters (available with Version 3.1).
array(
'key' => 'color', //(string) - Custom field key.
'value' => 'blue', //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN')
'type' => 'CHAR', //(string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
'compare' => '=', //(string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default value is '='.
),
array(
'key' => 'price',
'value' => array( 1,200 ),
'compare' => 'NOT LIKE'
)
So following the above example I would maybe try something like this and add as many keys as you need to in order to search all the taxonomies you want.
$args['meta_query'] = array(
array(
'key' => my_key_1,
'value' => $args['s'],
'compare' => 'LIKE',
),
array(
'key' => my_key_2,
'value' => $args['s'],
'compare' => 'LIKE',
),
array(
'key' => my_key_3,
'value' => $args['s'],
'compare' => 'LIKE',
)
);
return $args;
You would need to add a tax_query https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
The topic ‘ACF Relationship Field Search Filtering – Also look for taxonomies’ 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.