Support

Account

Home Forums Backend Issues (wp-admin) Relationship field with filter for custom field

Solved

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.

  • 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

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

The topic ‘Relationship field with filter for custom field’ is closed to new replies.