5

i have been using this code to get all fields from a specific group:

<?php
$GroupOrPostSlug = 'acf_specialgroup';
//or insert the ID of your fields Group.
$groupID='';



global $wpdb;
if (empty($groupID))
{$groupID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name =  '$GroupOrPostSlug' ");}
if (empty($groupID))
{$groupID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title =  '$GroupOrPostSlug' ");}

$custom_field_keys = get_post_custom_keys($groupID);
foreach ( $custom_field_keys as $key => $fieldkey )
{
    if (stristr($fieldkey,'field_'))
    {
        //echo $key . " => " . $fieldkey . "<br />";
        //echo $field['label'] . ': ' . $field['value'];
        //echo $fieldkey . "<br />";
        $field = get_field_object($fieldkey, $groupID);
        echo $field['label'];

    }
}
?>

but it displays only fields' names. doesnt there exist a simple hook/function, something like this: get_all_fields_from_group($id)

9

Thank you so much for your post, I spent half day to figure out how to get field names by their group.

If you have field names, you can easily get their values: get_field($field['name']);

EXAMPLE HOW TO GET IMAGES FOR SLIDER

    <?php 
                    //or insert the ID of your fields Group.
                    $groupID='116';
                    $custom_field_keys = get_post_custom_keys($groupID);
                    foreach ( $custom_field_keys as $key => $fieldkey )
                    {
                        if (stristr($fieldkey,'field_'))
                        {
                            //echo $key . " => " . $fieldkey . "<br />";
                            //echo $field['label'] . ': ' . $field['value'];
                            //echo $fieldkey . "<br />";
                            $field = get_field_object($fieldkey, $groupID); 

                            $acf_field_name = $field['name'];

                            $attachment = get_field($acf_field_name);
                            echo "<img src='".$attachment['url']."' title='".$attachment['title']."'/>";


                        }
                    }

?> 

Thanks again!

  • Just a note why this works: ACF uses wordpress-native custom post types and meta values ("post customs") :) – jave.web Jun 19 '16 at 0:10
3

Here's a function I created to get a group's fields:

function my_acf_get_fields_in_group( $group_id ) {
    $acf_meta = get_post_custom( $group_id );
    $acf_fields = array();

    foreach ( $acf_meta as $key => $val ) {
        if ( preg_match( "/^field_/", $key ) ) {
            $acf_fields[$key] = $val;
        }
    }

    return $acf_fields;
}
1

In case anyone stumbled upon this page like I did, and tried to figure this out, these methods won't work anymore with Version 5 and above of Advanced Custom Fields as it's usings posts instead of postmeta.

In that environment, this is the way I was able to get fields from a group and use them as values for a select field:

function acf_load_select_choices( $field ) {

  global $wpdb;
  $group_slug = 'Name or slug of the group';

  $group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name =  '$group_slug' "); 
  if (empty($group_ID))
    $group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title =  '$group_slug' ");

  $field['choices'] = array();
  $fields = acf_get_fields_by_id($group_ID);

  // to see what $fields contains, use:
  echo '<pre>'.var_dump($fields).'</pre>';

  if( $fields ) {
    foreach( $fields as $fieldrow ) {
      $field['choices'][ $fieldrow['name'] ] = $fieldrow['label'];
    }
  }

  return $field;

}

add_filter('acf/load_field/name=selectfieldname', 'acf_load_select_choices');
1

Since ACF uses custom posts of type 'acf' as groups, we can use get_page_by_title to retrieve the group's ID, and then do the rest. Here's a small utility function:

// This was tested with ACF free edition v4.4.11
function get_group_fields($group_name){
  $group = get_page_by_title($group_name, OBJECT, 'acf');
  if(empty($group)) return false;

  $meta = get_post_meta($group->ID);
  $acf_fields = array();

  foreach($meta as $key => $value){
    $acf_meta_key = stristr($key,'field_'); // acf fields all start with "field_"
    if($acf_meta_key) $acf_fields[] = get_field_object($key);
  }

  return $acf_fields; // returns an array of field objects
}

Usage:

$group_fields = get_group_fields('My marvelous group');

foreach($group_fields as $field){
   $label = $field['label'];
   $value = $field['value'];
   // etc...
}
0

If someone is still searching. You can just do it like this:

<?php 

      $fields = get_field_objects();

      if( $fields )
      {
        foreach( $fields as $field_name => $field )
        {
            echo '<div>';
                echo '<h3>' . $field['label'] . '</h3>';
                echo $field['value'];
            echo '</div>';
        }
      }
?>
  • 1
    Please take a minute and explain what is going on in the code instead of dumping a code snippet. – DᴀʀᴛʜVᴀᴅᴇʀ Oct 31 '17 at 18:20
  • Well, it just about the answer to the Question : "how to get field names by their group." And also a values in here; The snippet is much self explanatory – Artemiy Egorov Dec 12 '17 at 10:47
  • Awesome! Don't know why they don't include get_field_objects in their documentation! – Protector one Mar 25 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.