You are already using the correct Gutenberg/JS code, but it’s a limitation in the REST API which exposes the parent field only for hierarchical post types like page. But you can force the field to appear in the REST API responses via register_rest_field() — example for a my_cpt post type:
register_rest_field( 'my_cpt', 'parent', array(
'schema' => array(
'description' => __( 'The ID for the parent of the post.' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
),
) );
Alternatively, you can use the rest_prepare_<post type> hook to just add the parent in the response:
add_filter( 'rest_prepare_my_cpt', function ( $response, $post ) {
$data = $response->get_data();
$data['parent'] = $post->post_parent;
$response->set_data( $data );
return $response;
}, 10, 2 );
But if you want to allow editing the parent via the REST API, then the first option is preferred.
Related Posts:
- WordPress Rest API only returns content when posttype has editor capability
- Save the post parent in a custom post type in Gutenberg
- Combine multiple getEntityRecords calls
- How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
- Enable Gutenberg on custom post type
- How to enqueue scripts on custom post add/edit pages?
- How to set parent-child relationship between differents custom post types
- Integrating a custom post type into a page hierarchy
- Nested custom post types with permalinks
- Change custom post type to hierarchical after being registered
- Creating conditional blocks for WordPress Gutenberg
- Add class to Gutenberg Editor for Custom Post Types
- How To Limit Hierarchical Pages Depth (For Custom Post Types) To Children Only
- Display a list of child posts on parent posts of a custom post type
- Custom Post Type as Page parent
- How to filter by custom post type on Gutenberg “Latest Posts’ block
- How to handle a hierarchy with custom post types
- Can I have Post Types under other Post Types in Admin menu?
- Gutenberg: Sidebar for specific post type
- set a CPT as parent of a page
- Hierachical Custom post types permalinks not working
- Gutenberg how to make attribute to save to meta
- How to add button to post page WordPress 5.x
- Child pages on hierarchical Custom Post Types 404s
- How can I combine posts of different types in one hierarchy?
- Custom Post Type ‘hierarchical’ Help!
- How to check if last uri segment is a custom post type or taxonomy term?
- Custom permalinks – post type – hierarchical taxonomy’s
- How to show term-specific post list, without posts associated with child terms?
- WordPress Gutenberg-Block with ESNext (withState, withSelect)
- URL Design for Sub-Posts?
- The editor has encountered an unexpected error. // TypeError: Cannot read property ‘prefix’ of null
- How to query for posts (in hierarchical custom post type) that have children?
- Display different gutenberg template from selected post attributes
- Getting taxonomy terms used by custom post type
- Calculate Repeater Meta Box Input Field Values and Display Total
- How to localize value of posts
- Custom Taxonomies: Multiple Hierarchical Permailnks For A Single Post + Prev/Next Links
- How to create a repeatable / reusable divs in the metabox?
- Custom Scripts per page or Custom Post Type
- How do you use JavaScript to detect the homepage
- wp_list_pages doesn’t work in hierarchical custom post type
- wp is not defined error using wp.media to create a custom image uploader
- custom post type index page
- Post type hierarchy
- Hierarchical or Non-hierarchical Custom Post Type in Real World Example
- Detect if 1st, 2nd or 3rd level custom page?
- Lock post meta block at the bottom of a custom post type
- Hierarchical Custom Post Types or Similar
- Custom post type routing with hierarchy
- How can I list all custom post types in a block
- Parent / Child formatting in a list of post of a custom post type
- Show hierarchical relationship between to custom post types on admin list
- Custom post type hierarchical permalinks not behaving as expected for parent/child pages
- Gutenberg Featured-Image-panel missing when user with custom role edits Custom Post Type
- Adding a Gutenberg-like custom field on a custom post type
- How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
- How to Combine Two Custom Post Types into Single Permalink Structure
- Custom Posts Hierarchy? (State/City/Restaurant)
- Custom Post Type with two hierarchical Custom Taxonomies: strategy to generate best permalink structure
- Automatically populate a hierarchical taxonomy from a custom field
- Hierarchical Custom Posts – Highlighting Current Post in Sub-Menu
- Classic editor showing in Gutenberg when creating a custom post type
- Creating template with h3 block (block editor)
- How To Enable Block Editor (Gutenberg) for Existing Post Type via functions.php
- How to make a custom taxonomy selectable in post publish area?
- Remove base slug in permalinks of hierarchical custom post type
- Modifying Custom Post Type after registration (will it affect content?)
- show_in_rest false disable Gutenberg
- I can’t choose the custom taxonomy in my custom post type on Gutenberg
- Hierarchical Custom Post Types – Show only parent on tax archive?
- Twenty Fifteen: Adjacent posts by menu_order [duplicate]
- Display custom post type in hierarchical order with get_terms
- Open div inside a custom post-type with Fancybox
- Should I use custom taxonomy or custom post type
- Very simple wordpress block to display posts from an api call
- Parent field missing from custom post type in the block editor
- post_type_link not working in gutenberg
- post_row_actions not working for hierarchical post type
- Storing posts from query and accessing later via AJAX call
- Push metadata in array
- Custom Post Type / Two level deep taxonomy (cat. and subcat.) uri problem (error 404)
- Hierarchical Custom Post Types in Array
- WordPress Custom Post Type Children Template
- Use a hierarchical custom post type for a main blog and subblogs?
- What is the recommended / best way to do this: Event calendar post/type in a block-based theme?
- Getting meta in editor plugin, and event triggering issue
- How to create custom url structure based on custom post type rewrite slug, taxonomy and category slug?
- Plugin – Combine Meta Box Input Fields into single saveble record
- WordPress CPT save metadata in gutenberg
- JS innerhtml changing style when using AJAX
- Can I override a CPT template on a per post basis with Gutenberg block editor?
- How to set a CPT to have a page as a parent, then rewrite urls accordingly?
- Query Post Object in WP Admin
- How to display “Category and Post_tag” component in a CPT Gutenberg edit screen?
- Side effects of Script and Iframe in post
- Rewrite Rule for showing Parent/Child Relationship between Two Hierarchical Custom Post Types
- Need help with permalink rewrites for Custom Post Types with hierarchical relationships
- Rewrites/query for multiple hierarchical custom post types
- A case for Hierarchical Custom Posts