Support

Account

Home Forums ACF PRO Programmatic Location Rules

Solved

Programmatic Location Rules

  • Hi there!

    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

    1. 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.
    2. 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?

    Thanks in advance!

  • I just gave an basic example of this filter here https://support.advancedcustomfields.com/forums/topic/hide-elements-for-specific-user-roles/

    When I want to set things in the group what I do is export one to php so that I can see an example and then try to copy the example.

  • Hey John!

    Thanks for pointing me in the right direction, this is what’ I’m using and it seems to accomplish what I need.

    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;
    };
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Programmatic Location Rules’ is closed to new replies.