Support

Account

Home Forums ACF PRO Field always returns NULL

Solved

Field always returns NULL

  • Hello,

    I purchased PRO version yesterday.

    I need to show fields divided into groups. Similar to this thread.

    Used this code:

    $groups = acf_get_field_groups(array('post_id' => get_the_ID()));
    foreach( $groups as $group_key => $group ) {
    $fields = acf_get_fields($group);
    									if($fields ) {
    foreach( $fields as $field_name => $field ){
    echo  $field['value'] ;
    }
    }
    }

    Unfortunately, it doesn’t return any value. I’ve done var_dump in $fields foreach, and it is loading the right field, only problem is that “value” is always NULL.

    I have tried to get the value directly – the_field( “text_field” ); – and it is working fine. Any ideas?

    tnx

  • Hi @pero

    I’m afraid acf_get_fields() will always return an empty value. Instead, please use the get_field() function to get the value like this:

    $groups = acf_get_field_groups(array('post_id' => get_the_ID()));
    foreach( $groups as $group_key => $group ) {
        $fields = acf_get_fields($group);
        if($fields ) {
            foreach( $fields as $field_name => $field ){
                echo get_field($field['name']);
            }
        }
    }

    Hope this helps!

  • Thank you, this solved my problem.

  • Hi there, I could not get this to work with my code. Can you help? How could I get the values?

           <?php $fields = acf_get_fields('56'); 
            ?>
    
    <?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>';
        }
    } 
    
    } 
    ?>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.