-2

I would like to load ACF fields for custom post types based on a set of conditions and NOT use Custom Location Rules. A brief use case / rationale:

  • If CPT A exists AND Theme Option Y is 1, then show Field Group 1 on CPT A in wp-admin
  • I want to set rules programmatically because I don't want users to be able to override/mess up location rules via the field group options.

I've been poking around and according to some threads, it seems like hooking into acf/get_field_group is going to be the way to go, however it isn't documented.

Is there any way I can do a filter hook to override a field group's location rules based on programmatic conditions?

closed as off-topic by Mark Kaplun, shanebp, Jacob Peattie, bosco, Dave Romsey Sep 12 '18 at 17:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This forum doesn't cover programming for plugins. Contact ACF with this one. – De Coder Sep 10 '18 at 8:57
  • I understand and have actually contacted ACF, who recommended that I post here. If you look in StackOverflow, you will find many other ACF-related posts that did not get docked negative points. While I feel like this standard of "threads about plugins" gets applied inconsistently, I will try to respect it and will post elsewhere. – Ryan Dorn Sep 11 '18 at 7:55
1

In case anyone needs it, this is working for my needs:

add_filter('acf/get_field_group', 'my_change_field_group');
function my_change_field_group($group) {

    $get_current_screen = get_current_screen();
    $get_current_post_type = $get_current_screen->post_type;
    $my_option = get_field('my-option','option');

    if (
        $get_current_post_type == 'my-cpt' &&
        $my_option == 'stuff' &&
        $group['key'] == 'group_123456789'
    ) {
        $group['location'] = array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'my-cpt',
                ),
            ),
        );
    }
    return $group;
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.