Support

Account

Home Forums General Issues get_field not returning values

Solving

get_field not returning values

  • 
    if( get_field('enable_box') ){
      //Table with other ACF field values
    }
    

    enable_box is a true/false field. Whether checked or not, the code block does not appear. It’s a simple if statement, so what am I missing?

    • Edd

    • June 1, 2016 at 11:34 pm

    If the field is within a repeater or flexible content field, you’ll need to use get_sub_field() instead of get_field().

    If the field is within an options page or if you’re loop has a different post ID, you’ll want to use the second parameter of get_field as either:

    get_field('enable_box','option');

    or

    get_field('enable_box', $post_id );

    If it’s not either of those, add an image of your acf field setup and we should be able to see what’s happening.

  • Those didn’t work. Here’s a couple of screen shots:

    • Edd

    • June 2, 2016 at 1:57 am

    Sorry – those don’t work as they require authentication to see. Can you just upload them to your post through the little attachment icon in the bottom left?

  • Sorry. I did not see that there before!

  • Hi @randalthesecond

    I’m 99% sure your issue is that you attempt to get the value outside of it’s context. That means that you might be trying to fetch it outside of the loop. Or from a term/user/object that’s not a post object and thus need the second parameter.

    Just throwing in $post_id won’t help you. what @imaginedd is trying to say here is that you probably need to specify this second parameter and if it’s a field belonging to a post object you have to find the posts ID and pass it as the second parameter. For example, if you’re working outside the loop and not in a single.php or page.php you might get away with:

    
    global $post;
    if( get_field('enable_box', $post->ID) ){
      //Table with other ACF field values
    }
    

    We can’t help you further without more information about your setup tho so get those images working 🙂

  • just saw the images 🙂 We need to know the context of this field. Where do you place it? Where are you trying to fetch it? (in single.php, index.php, a widget etc. etc.) .

  • It’s in a content-part called content-post_feed.php which is used to create the gray boxes (Test Post #1, Hello World) on the home page of this site: http://klenk.everhard.dev.wrlweb.com/ (if you’re outside the US you may have trouble viewing that link)

  • Thanks. Tho I need to see some code since that does not tell me too much about it 🙂

    you can post code using the “backticks”: `
    there’s also a button above the comment field.

    If there’s ALOT of it and you’re not sure what to paste you can use a service like pastebin.org

    • chi11ax

    • August 24, 2018 at 8:44 pm

    ** edit **

    Ok, so weird but I reloaded it 100s of times and it did not work. Without changing a line of code, when I posted my complaint below, the next time I reloaded, it worked. And I’m using the wp-cli wp serve so I’m sure nothing is cached.

    Anyhow …

    *****

    Hi there! I have a similar problem, but mine is that it works fine if my page is http://site.com/front-page

    But when I set the front page to a static page via wordpress, so that it will access that page from
    http://site.com

    Then it fails and get_field(‘fieldname’) doesn’t return anything. Any pointers?

  • get_sub_field(‘whatever’) is not working for me when in Preview mode, and the page is Draft. When I publish it it works. When go back to draft they are empty.

  • Hello, I have a similar problem with “get_field (‘event_date’)”, it does not return anything. I try to get the month and day of the event (Events are a custom post type). I have already tried more than 5 or 6 methods and I can not solve it.

  • I use get_post_field and it works…

    • Leandro

    • October 17, 2019 at 10:57 pm

    Thank you!

  • This did the trick for me too! Same situation with a custom Date Picker field for events. Everything was coming back as false when I used get_sub_field() but get_post_field() got my true values to appear as they should.

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