Nebular Dust
Nebular Dust

Reputation: 403

ACF get_field not returning value

I'm trying to use get_field to return a simple text field and it returns empty for some reason. A field itself is where it should be and there is text in it, so that part is all set. This PHP code is loaded via php snippet, post thumbnail for example, shows up perfectly. So everything works except for ACF field value.

<div class="your-class">
    <?php
    $args = array(
        'post_type' => 'home_test',
        'posts_per_page' => -1,
        'orderby'   => 'name',
        'order'     => 'ASC',
    );
    $the_query = new WP_Query($args);
    $brand = get_posts($args);
    foreach ($brand as $post) {
        setup_postdata($post);
        $thumbnail = get_the_post_thumbnail_url($post->ID, 'full');

        $homelinkvalue = get_field("home_brand_link");

        if (!$thumbnail)
            continue;
        ?>
        <div>
            <p><?php echo $homelinkvalue; ?></p><img src="<?php echo $thumbnail; ?>">
        </div>

    <?php
    }
    wp_reset_postdata();
    ?>
</div>

Upvotes: 0

Views: 4445

Answers (2)

Nitesh Gour
Nitesh Gour

Reputation: 136

Try this one:

<div class="your-class">
       <?php
          $args = array(
                          'post_type' => 'home_test',
                          'posts_per_page' => -1,
                          'orderby'   => 'name',
                          'order'     => 'ASC',
          );
          $the_query = new WP_Query( $args );
          if ($the_query->have_posts) :
              while($the_query->have_posts) : $the_query->the_post();
          ?>
       <div>
          <p><?php the_field( "home_brand_link" ); ?></p>
          <img src="<?php the_post_thumbnail_url(); ?>">
       </div>
       <?php
          endwhile;
          wp_reset_postdata();
          endif;
          ?>
    </div>

Upvotes: 0

Joshua T
Joshua T

Reputation: 2599

I think the issue is that you are mixing together a custom post loop (your foreach and setup_postdata()) but then are using functions like get_field(), which make use of the global post object. In this case, get_field() tries to lookup the field value by checking against the global $post, but it has not been properly set. See warning here about setup_postdata($post):

You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.

You can implement this in your code with a slight modification:

global $post;
foreach ($brand as $currPost) {
    $post = $currPost;
    setup_postdata($post);
    // Rest of code as normal
}

Or, since get_field() can accept a specific post as an argument instead of automatically using the global one, you could change:

$homelinkvalue = get_field("home_brand_link");

to:

$homelinkvalue = get_field("home_brand_link",$post->ID);

Side note: Normally, the recommended way to iterate posts is with the special "WP loop" pattern, something like:

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <!-- Do something -->
<?php endwhile; ?>

Using the above pattern automatically sets the global $post variable as it loops through, which allows devs to use functions like get_field() without having to worry about explicitly passing in a specific post; makes things a bit easier.

Upvotes: 7

Related Questions