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


Finding Post ID in Gutenberg Block Templates within WordPress Preview

  • So I have discovered that you need to pull in


    And reference it within any calls to ACF’s

    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



    global $post;
    $post = $post_object;
    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;
    • Ars

    • July 22, 2019 at 9:55 am

    Function doesn’t work in preview page

    • nathanp

    • July 23, 2020 at 11:40 am

    This worked great for me, thank you.

Viewing 4 posts - 1 through 4 (of 4 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.