Home › Forums › Backend Issues (wp-admin) › Relationship field with filter for custom field
Hello,
Is there a way to add a “filter for meta” function to the relationship field just like the built in filter for taxonomy and filter for post type, so that only posts with a certain custom field would show up in the select area?
Thanks.
You can do this using the acf/fields/relationship/query filter https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
I’m sorry for getting back to this late. Thank for you pointing me to the right direction, it quickly solved the issue.
I think I have the same question as the above user. If only I knew what to do next…
I am using ACF to add a custom checkbox to my personnel posts to I can identify what type of personnel they are (e.g. ‘writer’, ‘graphic_designer’).
On another type of post, I would like to add a custom relationship field that allows me select only a specific type of personnel when I search. I thought this forum topic was the answer.
I opened functions.php and put the below-referenced code into that. I thought maybe if I went back to the Custom Fields group/field and try again to configure my field that ‘playwright_query’ would show up in the “filter by Post Type.” No such luck. What’s the next step? Is what I am trying to do even possible?
——— CODE ———–
function playwright_query( $args, $field, $post_id ) {
// only show children of the current post being edited
$args[‘meta_query’] = array(
array(
‘key’ => ‘personnel_type’, // name of custom field
‘value’ => ‘”playwright”‘,
‘compare’ => ‘LIKE’
)
)
// return
return $args;
}
// filter for every field
add_filter(‘acf/fields/relationship/query’, ‘playwright_query’, 10, 3);
———————————–
Okay, I figured it out.
It was just applying the filter to all of my select lists. I updated the add_filter to:
add_filter(‘acf/fields/relationship/query/name=playwrights’, ‘playwright_query’, 10, 3);
Now it does exactly what I needed it to do!
Thanks
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 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.