Support

Account

Home Forums Front-end Issues Retrieving saved ACF values

Solved

Retrieving saved ACF values

  • Relatively new to ACF and WordPress (20 years of PHP/Perl/MySQL) and trying to understand how ACF/WP pulls in meta_values from wp_postmeta.

    1. I set up a test page, ACF group, and related ACF fields on a sandbox site. All the data saved by ACF in the post appeared on the page as advertised.

    2. I tried do the same on an existing site, but this time as a CPT called intro that is used fire the form field on the admin side. The data is saving fine but no matter on what page template I place the_field, nothing appears. Using:

    <?php the_field('menu_intro'); ?>

    The wp_postmeta shows:

    4293 | menu_intro                  | This is the menu intro
    4293 | _menu_intro                 | field_5c5ba4f757dab   

    Is there some way that I have to “register” that this particular field appears on this particular page, or is the_field indiscriminate about this and just pulls it from the postmeta table regardless? (the latter is not working).

    I was able to pull it in using the $post_id as an arg, but had to write a wpdb query to get the id from the postmeta meta_key (awkward).

    Thanks, Brad

  • So, you have created a field, assigned it to your custom post type, and it shows up in the post editing screen, right? You have entered a value and saved it, and it doesn’t show up on the actual post/page?

    It sounds like you’re not pulling the right template or something. Can we see a demo and/or get more info about your theme set-up?

  • 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
      }
    }
    
    
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.