Support

Account

Home Forums Front-end Issues Get sub field object of field in a group? Reply To: Get sub field object of field in a group?

  • Well… I’m still struggling…
    I easily managed to get the label : $field[‘label’]
    It’s a progress, but I still can’t get the value.

    My goal is to receive an email with all the field labels + values when a form is submitted. It should be a common request but I couldn’t find anything about it.

    Here’s my full code :

    
    add_filter('acf/save_post', 'create_post_from_form');  
    
    function create_post_from_form($post_id) {
    
    	$to = '[email protected]';		
    	$subject = 'test notification 8';
    	$message = '';
    		
    	$fields = get_field_objects($post_id);
    
    	foreach($fields as $field) {
    			
    		$message =  $message . ' <strong>' . $field['label'] . '</strong><br>';
    			
    		$key = $field['key'];
    			
    		while (have_rows($key, $post_id)) {
    		// always happens once on a group field
    		$the_row = the_row(); // the row returns the row
    		// $the_row will be an array of "field_key" => "value" pairs
    		foreach ($the_row as $field_key => $value) {
    			$subfield = get_field_object($field_key,$post_id);
    			$value = get_field($subfield['name'],$post_id);
    			// now get the label and value and whatever else you need from $field;
    			$message =  $message . ' <strong>- ' . $subfield['label'] . '</strong> : '. $subfield['value'] .'<br>';  
    		}
    	}
    }	
    	
    
    $message = $message;
    wp_mail( $to, $subject, $message );	
    
    }

    The first foreach allows me to get the labels of the several acf-field-group. And thanks to you I managed to get the labels of the subfields that these groups contain. But not the values.