Support

Account

Home Forums General Issues Field Types from Field Groups

Solved

Field Types from Field Groups

  • Hi,

    How would I go about getting the values of all the fields that were of the Field Type ’email’ from a particular Field Group?

    For example, I have extended the User Profile editing screen to include a few more fields. Three of those fields are of the field type ’email’. So, I want to be able to look into this particular Field Group, find all the fields that are of the type ’email’ (3 in my case as stated above) and loop through their values, outputting them.

    Thanks 🙂

  • Are you trying to get all of the values from all users or just one user? It’s going to be difficult without knowing the user id and without knowing the field names to look at. You can get the field groups if you have a user id and loop through the fields in that group, but without a user id there is nothing built into ACF to get the fields. You could construct an SQL query and get the values from the usermeta table (https://codex.wordpress.org/Class_Reference/wpdb) but you’ll need to have the field names.

  • Hi John,

    EDIT: I should’ve specified, all this action should happen inside functions.php, not on the front end of the site.

    Thanks for your reply but no – its nothing to do with users I just want to get the value of the name attribute of every field that is an email type field from a certain Field Group.

    So I have a Field Group I’ve called ‘Additional Fields for User Profile’ which is – as the name suggests – some extra fields for the User Profile page. Some of these are email fields with their Field Type set to ’email’ – what I want to do is get the value of the name attribute of every field that has a type of email so:

    <input id="acf-field-contact_email" class="email" name="fields[field_52e10709850b3]" value="[email protected]" placeholder="" type="email">

    for this field (which has a type of email) I need to get the value: fields[field_52e10709850b3] from the name attribute.

    Thanks

  • You can get the field group from its key

    $group = acf_get_field_group($group_key)

    You can then loop through the fields, if you have nested fields (repeaters etc) then you’ll need nested loops or a recursive function

    
    $field_names = array();
    foreach ($group['fields'] as $field) {
      if ($field['type'] == 'email') {
        $field_names[] = $field['name'];
      }
    }
    
  • Hi John,

    Well, after a couple of hours wondering why your perfectly logical code wasn’t working, it turns out that my client is still using ACF4 and I just assumed (yup, I’m an ass) it was v5.

    So, anyway, until they’re happy to upgrade to v5 here’s my working v4 code which is very similar to yours – the main difference being the apply_filters on line 3:

    $groupID = 92;
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $groupID);
      if($fields) {
        foreach( $fields as $field) {
          if ($field['type'] == 'email') {
            echo "<li>" . $field['label'] . "</li>";
          }
        }
      }

    Thanks for your help, it definitely made things a lot easier.

  • Yep, ACF4 used a filter. I was assuming you were using 5 as well. I should have asked. Glad you got it worked out.

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

The topic ‘Field Types from Field Groups’ is closed to new replies.