Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)

share|improve this question
add comment

2 Answers 2

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;
}
share|improve this answer
add comment

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!

share|improve this answer
    
This was REALLY useful! Thanks. –  Jake Sep 7 '13 at 3:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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