Support

Account

Home Forums Backend Issues (wp-admin) Adding Rules to field group with php Reply To: Adding Rules to field group with php

  • I’m trying to do something similar to this. My code runs across different environments, and in each environment, the page I’m trying to set as a page parent location rule will probably have a different ID. So rather than having to manage that all over the place, I’d rather run a filter to alter the location rules. I have this going to grab the ID of the page by its path. And it seems to actually properly alter the location rules, but it doesn’t actually affect display in the admin screens. I feel like I’m doing something wrong, but I can’t quite put my finger on it.

    
    add_filter('acf/load_field_groups', 'add_page_parent_rule');
    function add_page_parent_rule($field_groups) {
        $parent_page = get_page_by_path('path/to/parent/page');
        foreach ($field_groups as &$field_group) {
            if ($field_group['key'] === 'group_6983f5557e555') {
                if (isset($field_group['location'][0])) {
                    $field_group['location'][0][] = array(
                        'param' => 'page_parent',
                        'operator' => '==',
                        'value' => (string) $parent_page->ID,
                    );
                }
            }
        }
        return $field_groups;
    }