Support

Account

Home Forums Add-ons Repeater Field acf/fields/post_object/query inside repeater filtered by taxonomy sub_field Reply To: acf/fields/post_object/query inside repeater filtered by taxonomy sub_field

  • This is what worked for filtering inside the Taxonomy Term Editor ( no repeater )

    add_filter('acf/fields/post_object/query/name=courses_list', 'filter_by_category');
    function filter_by_category( $args, $field, $post_id ) {
         
        // $post_id comes in here as term_# so we need to remove 'term_' to get the term ID
        $prefix = 'term_';
        
        // Also if you are creating a new taxonomy, post_id = 'term_0' so then there's no point in adding a filter
        if ( 'term_0' != $post_id && substr( $post_id, 0, strlen( $prefix )) == $prefix ) {
            // Set $term_id to the ID part of $post_id
            $term_id = substr( $post_id, strlen( $prefix ) );
            
            // And adjust the query to filter by specific taxonomy term
            $args['tax_query'] = array(
                array(
                    'taxonomy'  => 'school',
                    'field'     => 'term_id',
                    'terms'     => array($term_id),
                ),
            );
        }
        return $args;
    
    }