Support

Account

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

Solved

Feature request: Enable field group by meta value

  • Hi everyone.

    I’m using ACF Pro in my every project.

    I’m using extensively a page-{slug}.php templates from hierarchy, but there is a problem with a slug. It’s not something we can rely on.

    If the user changes the slug of the page, obviously template stops working as it’s not proper template anymore.

    Because of this, I’m adding a meta to the post (it can be done via ACF or custom metabox with meta, doesn’t matter), with a separate “template-slug”, text field.

    This field is unlikely for the user to change, as it’s marked as do not touch in the description, or if added by hand (without ACF), it can be even hidden from UI for some users.

    To make it work I’m hooking into page_template_hierarchy like so:

    
    /**
     * Unique page slug option
     */
    add_filter("page_template_hierarchy", function ($templates) {
        $template = get_field('template-slug');
    
        if (empty($template)) {
            return $templates;
        }
    
        array_unshift($templates, "page-$template.php");
        return $templates;
    });
    

    All is good, except that in ACF field group edit, when targeting field group, I cannot use this “template-slug” meta as a rule, so I need to stick with targeting specific page.

    If only this would be possible, my solution would be 100% complete.

    P.S.

    Just in case anyone suggested custom page templates.

    I’m using custom page templates as well, in every project. But I’m using them as I believe are intended to be used, for pages that have a shared template.

    If I do have a page that is only one page specific, and I know this up front, I don’t want to pollute templates switcher with irrelevant templates, as it usually starts to grow and is just confusing for editors.

    Just a thing to consider.
    I’m also using this so much I can even contribute my time to make this happen, but I would like to know if there is any will to put it into the core of the plugin.

  • This can be done, to some extent, using custom location rules. https://www.advancedcustomfields.com/resources/custom-location-rules/

    The only real issue is that for meta values to work the post needs to be saved and ACF cannot automatically update the field groups on the fly if the value is altered.

    At one time I built a plugin that would do this, however, it was extremely slow because it checked all the fields and it won’t work at all now because of the changes to the ACF JS API. I’m sure that with some JS that the issue with auto updating could be fixed, but not something that I’ll have time for in the near future.

  • Wow! Thanks, @hube2

    I had no idea this option already exists.

    Actually, I can live with updating the page after saving meta as it will be done by developers, not final website users.

    Thanks a lot! I’m gonna dig into this right now!

  • Just an added bit of information. If you are generating field groups in PHP you can actually skip steps 1, 2 & 3 of the custom location rules doc. As long at the location rules set in the field group are done correctly all you need is the rule match filter. If you’re going to set these location in the admin then you’ll need to do most of it. I’ve never added custom rule operators.

  • 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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.