Support

Account

Home Forums Backend Issues (wp-admin) How can I filter a posts relationship field by the current category edit screen? Reply To: How can I filter a posts relationship field by the current category edit screen?

  • I had the same problem, wanted to limit current term in custom taxonomy in wp-admin.

    Here is my solution.

    // Show 2000 posts pre ajax call, change sort, limit to current taxonomy and term id
    add_filter('acf/fields/relationship/query/name=author_sort_list', 'authors_fields_relationship_query', 10, 3);
    function authors_fields_relationship_query( $args, $field, $post_id ) {
        
        $url     = wp_get_referer();
        $parts   = parse_url($url);
        parse_str($parts['query'], $query);
    
        $term_id = $query['tag_ID'];
        $taxonomy = $query['taxonomy'];
        if ( isset($taxonomy) && isset($term_id)) {
    
            $args['tax_query'] = array(
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'term_id',
                    'terms' => $term_id,
                    'operator' => 'IN'
                )
            );
        }
    
        $args['posts_per_page'] = 2000;
        $args['orderby'] = 'date';
        $args['order'] = 'ASC';
    
        return $args;
    }