Home › Forums › Front-end Issues › Get sub field object of field in a group?
I have a wysiwyg field in one of my flexible content blocks, and I call “wysiwyg” as a class for the block with the following code:
<?php
$content = get_sub_field('text');
$field = get_sub_field_object('text');
$fieldtype = $field['type']; ?>
<div class="<?php echo $fieldtype; ?>">
It works great, but I need to achieve the same thing with a wysiwyg field within an acf field group and I can’t get it to work the same way. This is what I have now:
<?php
$content = get_sub_field('twi_fields');
$text = $content['twi_text'];
$field = get_sub_field_object('$text');
$fieldtype = $field['type'];
?>
<div class="<?php echo $fieldtype; ?>">
To get the field object of a group field sub field you need to treat the group field as a repeater, not as an array.
while (have_rows('twi_fields')) {
// always happens once on a group field
the_row();
$text = get_sub_field('twi_text');
$field = get_sub_field_object('twi_text');
$fieldtype = $field['type'];
}
Hi,
This works, but how can i get all the sub_fields in the group?
In other words : I have a group of fields, and for all the fields in the group i want to display label and value (without having to specify their key or slug individually)
If you want to get the keys and field names then you can do something like this
while (have_rows('twi_fields')) {
// 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) {
$field = get_field_object($field_key);
// now get the label and value and whatever else you need from $field;
}
}
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.
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 );
}
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 );
}
You cannot use get_field() to get a sub field of a repeater.
$subfield = get_field_object($field_key,$post_id);
$value = get_sub_field($subfield['name'],$post_id);
but you don’t even need to do that because the value will be in
$subfield['value'];
Hi,
Sorry for the triple-post. I tried to delete them but they weren’t appearing on the thread (even tried in incognito mode).
Anyway, i figured right after posting that from your previous code :
foreach ($the_row as $field_key => $value) {
$field = get_field_object($field_key);
// now get the label and value and whatever else you need from $field;
}
I just had to use
$value
Thank you
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.