Home Forums ACF PRO Feature request: Enable field group by meta value Reply To: Feature request: Enable field group by meta value

  • Thanks, I actually made it work.

    The main issue though is custom rule can match only by comparing to an item from a specific set of items (so the field in group edit is always select).

    It won’t let me compare against any string, so using a text field as a slug is not an option.

    What I ended up doing is to change page slug type to “Select”, and provide in its choices a list of available unique slug pages.

    Then I can access such a list in acf/location/rule_values/meta filter dynamically, so if new unique page templates needs to be added, I’m doing so only in this one field group.

    Full code:

    add_filter('acf/location/rule_types', function ($choices) {
        $choices['Page']['meta'] = __('Page meta', 'sage');
        return $choices;
    add_filter('acf/location/rule_values/meta', function($choices) {
        // get fields from group where page slug is set up
        $fields = acf_get_fields('group_5c77ae796e08a');
        // check if first (and only field) is available
        if (! isset($fields[0]) || empty($fields[0]['choices'])) {
            return $choices;
        // get choices provided with this field
        return $fields[0]['choices'];
    add_filter('acf/location/rule_match/meta', function ($match, $rule) {
        $page_slug = get_field('template-slug');
        $rule_page_slug = $rule['value'];
        if($rule['operator'] == "==") {
            $match = ($rule_page_slug == $page_slug);
        elseif($rule['operator'] == "!=") {
            $match = ($rule_page_slug != $page_slug);
        return $match;
    }, 10, 3);

    And in group_5c77ae796e08a group, in the only field (template-slug) I’m adding these choices:

    services : Services
    estimate-project : Estimate project

    So, in the end, I can pick it up like this:

    Field group screen