Support

Account

Home Forums Front-end Issues All fields from Field Group except… using acf_form

Unread

All fields from Field Group except… using acf_form

  • I was trying to display a form on a webpage using acf_from and wanted to display all fields in a Field Group, except for 2 of them. acf_from attributes allow for whitelisting a field_group and specific fields to be displayed, but there’s no way to blacklist them. So I made a function to do it for me and thought I’d share here –

    
    function get_field_keys_from_group($group, $negate = array()) {
    	
    	$group			= (int) $group;
    	$keys			= array();
    	$all_field_keys		= acf_get_fields($group);
    	//	var_dump ( $all_field_keys );
    	foreach ( $all_field_keys as $field ) {
    		$key = $field['key'];
    		
    		if ( !strstr($key,'field_') )
    			continue;
    		if ( in_array($key, $negate) )
    			continue;
    		
    		$keys[] = $key;
    		//	echo $field['label'] . ': ' . $key .'<br>';
    	
    	}
    	return $keys;
    }
    

    You send this function a field group ID as the first parameter and it sends back an array of field keys that are in that group.
    You can optionally send the second parameter (as an array of field keys) – these will be excluded from the returned array.

    After you have the keys, you implement your form using the whitelist fields attribute. Usage example below –

    
    $field_group = 178;
    $negate = array(
    	'field_53c986735068c',
    	'field_53c986735068d',
    );
    $fields = get_field_keys_from_group($field_group, $negate);
    $attrs = array(
    	'post_id'	=> 123,
    	'fields'	=> $fields,
    )
    acf_form( $attrs );
    
Viewing 1 post (of 1 total)

The topic ‘All fields from Field Group except… using acf_form’ is closed to new replies.