Support

Account

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

Solving

Adding Rules to field group with php

    • lex713

    • January 11, 2021 at 3:59 am

    I am making a website where when a new user account is added it creates a new custom post type and adds an existing field group to that post type. So normally I would go into my field group called ‘Reviews’ and the key is ‘group_5fc70b388fe13’. Then scroll all the way to the bottom to location and add a new rule. Post type = Dog. I know how to create a field group using https://www.advancedcustomfields.com/resources/register-fields-via-php/. But I want to be able to update an already created field group and add a new rule every time I create a new user.

  • use the hook acf/load_field_group hook and modify the location rules.

    
    $field_group = apply_filters( 'acf/load_field_group', $field_group );
    

    To see how the location part of the group works export you group to PHP and see what is already in there.

    • lex713

    • January 17, 2021 at 3:51 am

    So I tried this and got nothing.

    
    function add_location() {
    
    	$field_group = apply_filters( 'acf/load_field_group', $field_group );
    
    	if ($field_group['key'] = 'group_5fc70b388fe13')
    	{
    			array_push($field_group['location'],
    				array(
    					array (
    						'param' => 'post_type',
    						'operator' => '==',
    						'value' => 'registration',
    					),
    				)
    			);
    		return $field_group;
    	}	
    }
    add_action('init', 'add_location');
    
    • lex713

    • January 17, 2021 at 3:52 am

    @hube2

  • 
    add_filter('acf/load_field_group', 'add_location');
    function add_location() {
      if ($field_group['key'] = 'group_5fc70b388fe13') {
        array_push($field_group['location'],
          array(
            array (
              'param' => 'post_type',
              'operator' => '==',
              'value' => 'registration',
            ),
          )
        );
        return $field_group;
      }	
    }
    
    • lex713

    • January 18, 2021 at 3:44 am

    @hube2 so I have the code written and it work. Only problem is I need to call when I press a submit button in a form. Is there anyway to call separate functions with different arguments. Or is there a way to write this code inside my other function. I just donโ€™t know if you can have an action and a filter in the same function.

  • The rules must be added every time the field group is loaded, it’s not something you can do once. The rules you add will have to be dynamically generated from something like what custom post types have been created.

Viewing 7 posts - 1 through 7 (of 7 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.