Support

Account

Home Forums Front-end Issues Retrieving saved ACF values Reply To: Retrieving saved ACF values

  • The problem is that you are not in “The Loop”. This is a WP concept that you may not be aware of since you are new to WP https://codex.wordpress.org/The_Loop and this is why you can get the value if you supply the ID. If you are outside of “The Loop” then ACF does not know what post to get the value from. You must have a loop, even for showing a single post, an all things related to this post must be done in “The Loop”, or you must supply the ID so that WP and ACF knows what post you are working with.

    
    // outside the loop
    // the post ID of the current post is not known
    $value = get_field('my-field'); // will return nothing here
    
    // The Loop
    if (have_posts()) {
      while (have_posts()) {
        the_post();
        // this is required even in single post templates
        $value = get_field('my-field'); // returns field value for this post
      }
    }