Support

Account

Home Forums Gutenberg How to get parent post ID in block template on edit screen?

Solving

How to get parent post ID in block template on edit screen?

  • Hi All,

    Attempting to do something and I’m not quite sure if it’s possible.

    What I’m trying to do is similar to this post: https://support.advancedcustomfields.com/forums/topic/how-to-get-current-post-id-in-block-template-on-edit-screen/ (I figured I would just copy the title since it’s an extension of that question), but instead of just the post ID, I’m trying to populate a block with the post parent’s title and a list of sibling pages based on the parent post ID.

    It works great on the front end with:

    global $post;
    return $post->post_parent;

    …but I can’t seem to get it to work for me on the backend.

    So my question is, is this event possible? If not, I may just have to manually create a field for the client to populate the parent page name and a list of sibling pages so that it will for sure show up on the edit screen as well as the actual page.

    Thanks in advance!

  • Because your in the editor, the current/global post isn’t the post your editing.

    I use this function (paste to functions.php) to get the current post ID.

    function maybe_editor_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;
    }

    I found this code somewhere else on this forum but I forgot where.

    Once you have the actual current post, you can find it’s parent by using wp_get_post_parent_id() or whatever method you prefer.

  • Thanks for the reply, Jelmertje. I actually linked to that post in my original post as I’ve been able to get that to work just fine, but can’t seem to get the parent post ID in the admin screen/editor no matter how hard I try, including trying to integrate wp_get_post_parent_id() with it somehow to get it in the admin screen.

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

The topic ‘How to get parent post ID in block template on edit screen?’ is closed to new replies.