Support

Account

Home Forums ACF PRO Get next field value in foreach loop through field group

Solved

Get next field value in foreach loop through field group

  • I have an open ticket but thought maybe someone can help with this.

    My client wants to be able to enter and save data in fields but have toggles to show or hide these on the front end.

    To accomplish this, I have True/False fields next to each field in the admin. Something like this:

    Data: __________________ (Text field) Show/No Show [X] (True/False field)

    In my template, I’m grabbing the field group, then using a foreach loop to iterate through the fields.

    Then, I want to check to see if the current field type is true_false and if it has a value of ‘1’, then show the data in the NEXT field. Here’s what I have (not working):

    
    $fields = acf_get_fields('204'); //my field group
    
    if( $fields ) { 
    
    foreach($fields as $field)
    {
        $array_copy = $fields;
        $value = get_field( $field['name'] ); // this works
        // $value = $field['value']; <-- this does not work because value is null
    
        $nextValue = next($array_copy);
        $nextValue = $nextValue['value']; // <-- this does not work because value is null
        // var_dump($value);
    
        echo 'Current: ' . $value . ' | Next:<span style="color:red">' . $nextValue . '</span><br>'; 
    
        if ($field['type'] == 'true_false' && $value == '1' ) {
        	echo $nextValue; // this doesn't work either (obviously)
    	}
    
    }
    ?>
    

    In the $fields array, why is $field['value'] null? Shouldn’t that hold the value?

    When I use var_dump($fields), all of the $field objects have 'value' => null.

    See attached for screenshots of the var_dump($fields);

    What can I use here to get the $nextValue?

    Thanks!

  • You are using acf_get_fields() which is a function in ACF that gets the fields in a group. It is an internal ACF function that builds field groups. This function does not populate the field values.

    To get he values in an array you would need to use get_fields() then you would need to match up the values with the names

    
    $fields = acf_get_fields('204');
    $values = get_fields();
    // some time later to get a value for a field
    $value = $values[$field['name']];
    
  • Hi John,

    This helps in terms of understanding why acf_get_fields(); returns null values however this doesn’t solve the more pressing issue of getting the value of the next item in the $fields array.

    Using var_dump($values); gives you all the fields from that page which is not really what I want here. I really want only the fields in the selected group. And this $values array doesn’t include the group that the fields are associated with.

    Thus, when in the foreach ($fields as $field) loop, it *does* retrieve the values however the only way to retrieve the NEXT item (which is really what I need) requires having the values in the initial array.

    Since there’s no there there, it won’t work.

    Maybe I could take a look at the acf_get_fields(); function and craft another one that retrieves the values as well.

    My ultimate goal is to add a show/no show toggle for each field. The client wants to be able to save the values with each post so it is visible in the admin but toggle the visibility on the front end.

    There are lots of fields and field groups for each post (these are from a “Properties” CPT) and there are 83 fields associated with each property in 7 groups. Not all of the fields will have toggles so that is why I was grabbing them by group.

    If there’s another way to go about this, I’m all ears. I thought about adding a special character as a flag (something like adding “%” at the beginning of the data) but this will only work for text or text area fields. We have radio buttons, checkboxes, drop downs as well.

    Any ideas?

    Thanks!

  • Just to clarify, this works:

    
    $show_hide_pricing_structure = get_field_object('show_hide_pricing_structure');
    $pricing_structure = get_field_object('pricing_structure');
    $show_hide_base_rent = get_field_object('show_hide_base_rent');
    $base_rent = get_field_object('base_rent');
    $show_hide_pricing_metric = get_field_object('show_hide_pricing_metric');
    $pricing_metric = get_field_object('pricing_metric');
    $show_hide_lease_type = get_field_object('show_hide_lease_type');
    $lease_type = get_field_object('lease_type');
    
    if($show_hide_pricing_structure['value'] == '1') {
    	echo '<tr>';
    	echo '<td class="detail-key">' . $pricing_structure['label'] . '</td>';
    	echo '<td class="detail-value">' . $pricing_structure['value'] . '</td>';
    	echo '</tr>';
    }
    
    if($show_hide_base_rent['value'] == '1') {
    	echo '<tr>';
    	echo '<td class="detail-key">' . $base_rent['label'] . '</td>';
    	echo '<td class="detail-value">$' . $base_rent['value'] . '</td>';
    	echo '</tr>';
    }
    
    if($show_hide_pricing_metric['value'] == '1') {
    	echo '<tr>';
    	echo '<td class="detail-key">' . $pricing_metric['label'] . '</td>';
    	echo '<td class="detail-value">' . $pricing_metric['value'] . '</td>';
    	echo '</tr>';
    }
    

    However this is not really easy to maintain and if the fields change or some are added, it doesn’t do the job.

    Looping through the fields seems like the right way to do this however I need to be able to access the next value in the array for it to work.

  • 
    $show_next = false;
    foreach ($fields as $field) {
      
      if ($field['type'] == 'true_false' && $values[$field['name']]) {
        // this is a true false field set to true
        $show_next = true;
      } elseif ($field['type'] == 'true_false' && !$values[$field['name']]) {
        // this is a true false field set to false
        $show_next = false;
      } elseif ($show_next) {
        // the field is not a true false field
        // but the last true false field was true
        // output field
      }
    }
    
  • Thanks @hube2. This logic works and is basically what I had but there is still no way to get the next element’s value from the array.

    There needs to be a way to grab all the field objects in a group with their values. Currently there isn’t as far as I can tell.

  • @hube2 this works!! I had a few things in the wrong places. Here is the final code:

    
    $fields = acf_get_fields('204');
    
    $values = get_fields(); 
    
    if( $fields ) { 
    	$show_next = false;
    	foreach ($fields as $field) {
    		$value = $values[$field['name']];  
    	  if ($field['type'] == 'true_false' && $values[$field['name']]) {
    	    // this is a true false field set to true
    	    $show_next = true;
    	  } elseif ($field['type'] == 'true_false' && !$values[$field['name']]) {
    	    // this is a true false field set to false
    	    $show_next = false;
    	  } elseif ($show_next) {
    	    // the field is not a true false field
    	    // but the last true false field was true
    	    // output field
    	    echo '<tr>';
    		echo '<td class="detail-key">' . $field['label'] . '</td>';
    		echo '<td class="detail-value">' . $value . '</td>';
    		echo '</tr>';
    	  }
    	}
    } 
    

    Thank you so much.

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

The topic ‘Get next field value in foreach loop through field group’ is closed to new replies.