Support

Account

Home Forums General Issues Possible bug with get_field()

Solved

Possible bug with get_field()

  • Hello,

    I am having issues with the get_field() function.

    It works good in my template parts like header.php, single.php, page.php but does not return any values when I use it in functions.php

    For example I copied this code in single.php:

    if( isset( $_GET['debug_acf'] ) ){
    
        global $post;
        
        // Have used both field_key and field_name
        $data = get_field( 'field_key', $post->ID );
        
        // Prints out the data
        print_r( $data );
    }

    This works, but when I copy the code inside functions.php:

    if( isset( $_GET['debug_acf'] ) ){
    
        global $post;
        
        // Have used both field_key and field_name
        $data = get_field( 'field_key', $post->ID );
        
        // Prints out nothing
        print_r( $data );
        
        // Function does exist
        if( function_exists('get_field') )
            echo 'get_field() exists';
    }

    The post ID is correct in both parts of the code, $data in my case is repeater data. But it does not work with normal text fields either.

    I am using ACF version: 4.2.2
    Repeater version: 1.0.1

    Got Gravity Forms activated and Admin Menu Editor plugin.

    • Elliot

    • August 29, 2013 at 2:31 pm

    Hi @icethrill

    Unfortunaltey, ACF needs to get past the init action for it to work (get_field / any api function)

    I’m hoping to find a work around in the future, but for now if you move your code within an action (init or later), then your value will work as expected!

    Sorry for the confusion.

    Cheers
    E

  • Alright, then I thought it was something like that. Thanks Elliot for the quick support!

Viewing 3 posts - 1 through 3 (of 3 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.