Support

Account

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

Helping

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;
    }
Viewing 2 posts - 1 through 2 (of 2 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.