Support

Account

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

Solving

Adding Rules to field group with php

  • 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.

  • 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');
    
  • 
    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;
      }	
    }
    
  • @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.

  • I tried to do this, but it won’t show up.

    1) https://ibb.co/mNy3QD9 (it works, it adds correctly to array)
    2) see here dump of $field_group https://ibb.co/wsL8dss
    3) it even correctly shows up when editing ACF https://ibb.co/DC7ZYPh

    But when I am in product, fields won’t show up. Any guess what’s wrong?

  • After some quality reverse engineering time, I found out I need to use filter “acf/load_field_groups”, in which I go group by group and set settings I need – https://ibb.co/M1T6HXM.

    For some reason “acf/load_field_group” and “acf/validate_field_group” does not work.

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

You must be logged in to reply to this topic.