Support

Account

Home Forums ACF PRO Filtering taxonomy on relationship picker

Solved

Filtering taxonomy on relationship picker

  • Hello,

    I currently have a filter successfully set on a relationship picker… however, the taxonomy select box still shows all of the available taxonomies. If I select any one of these options, it defaults to the filter override, regardless of what term is chosen. Is this a bug? Or am I filtering this incorrectly? The relationship picker is correctly showing the filtered values. Thanks!

    //filter to only show top level category of current cpt
    function generator_copyrepo_picker_filter( $args, $field, $post_ID )
    {
        $postType = get_post_type($post_ID);
    
        $args['tax_query'] = array( array(
            'taxonomy' => 'customergroup',
            'field' => 'slug',
            'terms' => $postType
        ));
    
        return $args;
    }
    //field_5735ef5e9b40b - copy repo
    add_filter('acf/fields/relationship/query/key=field_5735ef5e9b40b', 'generator_copyrepo_picker_filter', 10,3);
  • Hi @martinbethann

    The taxonomy query is built before the “acf/fields/relationship/query” is called. Because you add the query like this:

    $args['tax_query'] = array(...);

    It will replace the tax_query option that is created by ACF. Kindly try it like this instead:

    // Set empty array if there's no tax_query
    if( !isset($args['tax_query']) ) { $args['tax_query'] = array(); }
    
    // Push the query instead of replacing it
    $args['tax_query'][] = array(
        'taxonomy' => 'customergroup',
        'field' => 'slug',
        'terms' => $postType
    );

    I hope this helps 🙂

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

The topic ‘Filtering taxonomy on relationship picker’ is closed to new replies.