Home Forums Gutenberg get_field in custom_post_type in ACF Block returns NULL


get_field in custom_post_type in ACF Block returns NULL

  • Hello people, i would like to ask for some help, because i am stuck 2 days in this problem and i cannot find it anywhere, my get_field is returning NULL. I have the custom_post_type called exhibit-items and i am looping it in the gutenberg blocks, while trying to get the field of simple text it is returning null. Here’s the code itself.

    So just to be clear, i have the ACF Field in Custom Post Type called ‘print_opp_price’, i did set the ACF Field to post-type = exhibit-items AND block = “my-block”.

    $exhibit_args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘exhibit-items’,
    ‘post_status’ => ‘publish’,
    $exhibit_items_post_type = new WP_Query($exhibit_args);

    <?php while ($exhibit_items_post_type->have_posts()) : $exhibit_items_post_type->the_post(); ?>
    global $post;
    $price = get_field(‘print_opp_price’, $post->ID) ?>
    <h1><?php the_title(); ?></h1>
    //this is returning NULL <?php var_dump($price) ?>

    <?php endwhile;

  • Wouldn’t use global $post and $post->ID within a loop inside of a block. If you change it to:

    <?php $price = get_field('print_opp_price',get_the_ID()); ?>

    Does it work?

    Thinking $post->ID is pulling the ID of the page/post this block is on and looking for the field there vs. the post in the loop.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.