useCurrentPostId
Summary
Section titled “Summary”Returns the ID of the post currently open in the editor.
When to use / When not to use
Section titled “When to use / When not to use”Use it when a component needs the current post’s ID to query related data, e.g. fetching
sibling posts over the REST API. If you need the post type instead, use
useCurrentPostType. If the ID is already available as a prop or block attribute, do not
duplicate it with this hook.
Import
Section titled “Import”import { useCurrentPostId } from '@isudev/gutenberg/hooks';import { useCurrentPostId } from '@isudev/gutenberg/hooks/useCurrentPostId';Takes no arguments.
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
— |
— | — | — | This hook takes no arguments. |
Returns
Section titled “Returns”number | undefined as declared — the ID of the entity currently open in the editor, or
undefined when the editor has no post set (for example the widgets screen, or before the post
has loaded). The declared type is optimistic: in the site editor the id of a template is a
string (see Behavior).
core/editor’s getCurrentPostId() returns null in that empty case, not undefined.
The hook normalizes that null to undefined so consumers have a single absent value to
guard against — which is why the undefined checks below are the right ones.
Examples
Section titled “Examples”Fetching related posts over the REST API
Section titled “Fetching related posts over the REST API”const postId = useCurrentPostId();
const relatedPosts = useSelect( ( select ) => postId ? select( 'core' ).getEntityRecords( 'postType', 'post', { exclude: [ postId ], per_page: 3, } ) : undefined, [ postId ]);Handling the undefined case explicitly
Section titled “Handling the undefined case explicitly”const postId = useCurrentPostId();
if ( postId === undefined ) { return <Spinner />;}
return <PostMetaPanel postId={ postId } />;Behavior
Section titled “Behavior”- Reads
core/editor’sgetCurrentPostId()viauseSelect, normalizing the store’snulltoundefined. - It reports whatever entity the editor has loaded, not only a post. In the site editor that
is the entity being edited: a string template id such as
'twentytwentyfive//home', or a numeric page id when editing a page there. It does not returnundefinedin the site editor. - The declared return type is
number | undefined, so a template id arrives as a string despite the type. Do not assume the value is numeric without checking. - The value is empty only where the editor has no post set at all, e.g. the widgets screen or the first renders before the post resolves.
Styling
Section titled “Styling”Not applicable — this hook renders nothing.
Gotchas
Section titled “Gotchas”- A consumer must handle
undefined, not assume a number. Passing it straight into a REST query without a guard produces a request for post IDundefined. - A defined value is not proof that a real post is open. In the site editor you get the
template’s id, so a query built from it targets the template. Pair the check with
useCurrentPostTypewhen the code only makes sense for a specific post type.
Related
Section titled “Related”useCurrentPostType— the same post, by post type.