0

I'm trying to set a custom field featured_image with ACF and can't access it within my php code. First are screenshots of confirming adding the field to the latest post, then after is the php code.

I'm very new to WordPress so I'm expecting this to be a trivial misunderstanding.

I've run var_dump( get_post_meta(get_the_ID()) ); which doesn't show the existence of the 'featured_image' field. I've also created a text custom field, which also doesn't show up.

ACF field setup

Setting field value on post

<?php $my_query = new WP_Query( 'cat=2&posts_per_page=3' );
  while ( $my_query->have_posts() ) : $my_query->the_post();
      // some variable code
?>

    <div class="section-info background3">
      <div class="section-details">
        // some irrelevant html

        <?php

          $image = get_field('featured_image');
          var_dump( $image );
          echo $image;

          $featured_image = the_field('featured_image');
          var_dump( $featured_image );

          $size = 'full'; // (thumbnail, medium, large, full or custom size)

          if( $image ) {
            echo wp_get_attachment_image( $image, $size );
          }
        ?>
      </div>
    </div>

<?php endwhile; ?>

Which outputs

bool(false) for var_dump( $image );

NULL for var_dump( $featured_image );

Any help is greatly appreciated. Thanks in advance.

1 Answer 1

1

First strategy I'd go for in this case is to confirm that the data is present at the DB level.

Run a query against your DB:

SELECT * FROM `wp_postmeta` WHERE `post_id` = YOUR_POST_ID AND `meta_key` LIKE '%featured_image%'

If you can confirm that the data's there, then the next thing to do is ensure that you're passing the right post_id to get_field.

Because you're creating a custom WP_Query, and get_field usually infers ID from the global $post var, there's always potential for confusion. get_field optionally takes a second $post_id parameter.

If you can dump the contents of get_the_ID() inside your custom loop, and confirm that it matches the post whose featured_image field has the data, then send it along in the form of

<?php 

$my_query = new WP_Query('cat=2&posts_per_page=3');

while ($my_query->have_posts() ) :
    $my_query->the_post();
    var_dump(get_the_ID()); // <-- this should match the ID of the post w/ the featured_image
    $image = get_field('featured_image', get_the_ID());
    var_dump($image);
endwhile;

wp_reset_postdata(); // always good practice to reset the globals after a custom q!
Sign up to request clarification or add additional context in comments.

2 Comments

Your SQL query helped me find the issue. I had confirmed that 'featured_image' was in the DB, but didn't check that it connected to the correct post. Once I found out the post # was different, I realised I was using the wrong category (each post in category 2 has a verrrrrry similar post in category 4). Once I changed the category, I could then fetch the field. Thanks :)
Wonderful! Glad to hear it, Simon. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.