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?

  • Actually, I’m still struggling… I managed to get the label easily, but I still cant get the value.

    $subfield[‘value’] doesn’t seem to work.
    I also tried get_field($subfield[‘name’]) but it doesn’t work either.

    I’m simply trying to receive an email with all the field labels and values when a user submits a form.

    Here is my full function :

    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);
    				// 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 );	
    }