# \[solved\] PHP Block: How to get the post ID inside a custom block if in edit mode?

**URL:** https://forum.pinegrow.com/t/solved-php-block-how-to-get-the-post-id-inside-a-custom-block-if-in-edit-mode/8404
**Category:** WordPress
**Created:** [September 29, 2023, 8:14am UTC](https://forum.pinegrow.com/t/solved-php-block-how-to-get-the-post-id-inside-a-custom-block-if-in-edit-mode/8404 "2023-09-29T08:14:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)
#### Post date: [September 29, 2023, 8:14am UTC](https://forum.pinegrow.com/t/solved-php-block-how-to-get-the-post-id-inside-a-custom-block-if-in-edit-mode/8404/1 "2023-09-29T08:14:34Z")

</div>

I need to get the post ID in a Custom PHP Block while working in the editor.

This is not working:

```auto
global $post;
echo $post->ID;

```

Nor is this:

```auto
echo get_queried_object_id();

```

Also this does not work:

```auto
if(isset($_GET['action']) && $_GET['action'] === 'edit') {
    $post_id = $_GET['post'];
}

```

This is not working eihter:

```auto
get_current_screen()->is_block_editor()

```

Can this be that hard?

---

<div class="post-metadata">

### Author: ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)
#### Post date: [September 29, 2023, 8:21am UTC](https://forum.pinegrow.com/t/solved-php-block-how-to-get-the-post-id-inside-a-custom-block-if-in-edit-mode/8404/2 "2023-09-29T08:21:16Z")

</div>

This works - but it is not elegant. There must be a better way.

```auto
global $post;

if(!isset($post->ID)) {
    parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $queries);
    $post_id = $queries['post'];
}

```

---

<div class="post-metadata">

### Author: ![matjaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/matjaz/32/5_2.png) [@matjaz](https://forum.pinegrow.com/u/matjaz)
#### Post date: [October 3, 2023, 1:03pm UTC](https://forum.pinegrow.com/t/solved-php-block-how-to-get-the-post-id-inside-a-custom-block-if-in-edit-mode/8404/3 "2023-10-03T13:03:08Z")

</div>

@MichelyWeb try using the “Load edited post” option of the Block action. For dynamic posts, it loads the current post into the $post variable during the editing.

 ![load.post](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/7/7f8ee519720b844c776bb98838ccf2d18fa5fb5f.jpeg)
