Support

Account

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

Solving

How can I filter a posts relationship field by the current category edit screen?

  • Soooo I’m trying to:

    1) add a per-category relationship field to the edit screen for each category
    2) filter the posts returned in the relationship field on each category edit screen by the id of the category currently being edited. In other words, if I’m editing a given category’s edit screen, I want a relationship field containing only posts from that category.

    I’ve got part 1 working fine. Easy enough.

    Part two, however — I’m stumped.

    Here’s what I’ve got so far:

    function limit_cat_post_relationships( $args, $field, $post )
    {
        $args['category__in'] = $_GET['tag_ID'];
        return $args;
    }
    add_filter('acf/fields/relationship/query/name=ibm_topic_feat_posts', 'limit_cat_post_relationships', 10, 3);

    If I feed an explicit numerical cat id value to the argument $args['category__in'] = 44, for example, to test, it works — but I’m struggling to get that to happen dynamically based on the currently-being-edited category. $_GET['tag_ID']; doesn’t work, and I’m not sure how else to retrieve the id of the current edit screen from within the filter function.

    Any help figuring this out would be truly appreciated!!!!!

  • I think (but it’s only a guess) that the problem is $_GET[‘tag_ID’] is a string value and WP might require this to be a number. Try

    
    $args['category__in'] = intval($_GET['tag_ID']);
    
  • thanks — I’ll give that a shot and report back!

  • Needed this today, and found the solution.

    Here it is in case someone needs it:

    function limit_cat_post_relationships( $title, $post3, $field, $post_id  )
    {
        /* Because this is proccesd as an AJAX request
         * we can get the referer, and extract the tag_ID from there
         */
        
        $url     = wp_get_referer();
        $parts   = parse_url($url);
        parse_str($parts['query'], $query);
    
        $title['tax_query'][0]['terms'][0] = $query['tag_ID']; 
        return $title;
    }
    add_filter('acf/fields/relationship/query/name=ibm_topic_feat_posts', 'limit_cat_post_relationships', 10, 3);
  • 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;
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘How can I filter a posts relationship field by the current category edit screen?’ is closed to new replies.