1
 <?php $fields = acf_get_fields('6066'); ?> <?php if( $fields ) { foreach( $fields as $field) { $value = get_field( $field['name'] ); if ($value) { echo '<dl>'; echo '<dt>' . $field['label'] . '</dt>'; echo '<dd>' . $field['value'] . '</dd>'; echo '</dl>'; } } } ?> 

This is what I have. If I do a var_dump on acf_get_fields, it apparently sets the value to NULL. I could have known, as it's written here:

https://www.advancedcustomfields.com/resources/get_field_object/

The problem: I have to first get all fields in a specific field_group, hence I am using acf_get_fields.

I thought with using $field['value'] I could get it to work, but apparently this does not work.

Can someone help me to retrieve the values in the foreach per field? Surely there must be a way, right?

PS:

<?php $fields = get_fields(); if( $fields ): ?> <ul> <?php foreach( $fields as $name => $value ): ?> <li><b><?php echo $name; ?></b> <?php echo $value; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> 

This gives me ALL fields. I just need a specific list of fields from a specific field group. That is the problem.

I tried all suggestions here: ACF - get fields from group, but none of them do the trick for my case.

2 Answers 2

1
$specifications_group_id = 6066; // ID of the your group as you mentioned $specifications_fields = array(); $fields = acf_get_fields( $specifications_group_id ); foreach ( $fields as $field ) { $field_value = get_field( $field['name'] ); if ( $field_value && !empty( $field_value ) ) { $specifications_fields[$field['name']] = $field; $specifications_fields[$field['name']]['value'] = $field_value; } } print_r($specifications_fields ); 
2
  • Instead calling get_field for every field, you can use get_fields before foreach and look for field key in its result in the loop for performance. Commented May 4, 2023 at 12:12
  • Thank you, I'll try it out and let you know! Commented Dec 10, 2023 at 10:36
1

earlier I had to substitute fields from one post to another (related in the post object field) in the backend, but only for a specific group. In my case this code worked

// Get field from another post $another_post_id = get_field('Blocks-Benefits-copyBlock', $post_id ); $fields = acf_get_fields('group_6457959205b1b'); // Update fields in this post foreach ($fields as $field) { $field_name = $field['name']; $field_value = get_field($field_name, $another_post_id); update_field($field_name, $field_value, $post_id); } 
1
  • Thank you, I'll try it out and let you know! Commented Dec 10, 2023 at 10:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.