Support

Account

Home Forums ACF PRO Finding Post ID in Gutenberg Block Templates within WordPress Preview

Solving

Finding Post ID in Gutenberg Block Templates within WordPress Preview

  • So I have discovered that you need to pull in

    get_the_ID();

    And reference it within any calls to ACF’s
    the_field('fieldname',get_the_ID());

    in order to have a Gutenberg Block template accurately display ACF, as it seems that the ID has been flipped to the Block ID (and I totally get why).

    My issue is that I cannot get the Post ID (no way, no how) within the template so that the preview that is displayed within WordPress and Gutenberg is accurate. The functions

    get_the_ID();

    and

    global $post;
    $post = $post_object;
    setup_postdata($post);
    print 'Post: ' . $post->ID;

    Both come up with no values within the preview in WordPress/Gutenberg. It absolutely works when printing out the actual live page, but it doesn’t show anything within the WordPress interface.

    Any idea how to grab the Post ID within a Gutenberg Block template so that I can grab some ACF fields? ACF fields populate just fine if I feed it a static Post ID, I just can’t find a way to feed it the current Post ID.

  • Adding this function will solve the issue (compliments of another poster):

    function my_acf_post_id() {
    	if ( is_admin() && function_exists( 'acf_maybe_get_POST' ) ) :
    		return intval( acf_maybe_get_POST( 'post_id' ) );
    	else :
    		global $post;
    		return $post->ID;
    	endif;
    }
  • Function doesn’t work in preview page

  • This worked great for me, thank you.

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

The topic ‘Finding Post ID in Gutenberg Block Templates within WordPress Preview’ is closed to new replies.