Support

Account

Home Forums General Issues Set field group programatically Reply To: Set field group programatically

  • This fixed it which means I was wrong in my assumptions above. I just basically overwrote the existing locations array and everything works fine:

    add_filter( 'acf/validate_field_group', 'add_custom_location_dynamic', 10, 1 );
    function add_custom_location_dynamic( $field_group ) {
        if ( $field_group['key'] === 'group_6786e0728946a' ) {
    //      This line below clears any existing locations
            $field_group['location'] = array();
            $currentYear =  get_field('current_year', 'option');
            $awardArgs = createYearArray($currentYear);
    //      $awardArgs is just an array of ["2024", "2025"]
            $x=0;
            foreach($awardArgs as $year) {
    
                $field_group['location'][$x][] = array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'entrant_'.$year,
                );
                $x++;
            }
        }
        return ($field_group);
    }

    Hope that helps anyone else looking for this.