admin管理员组文章数量:1332383
It's a common practice to use the post_parent
property in the WP_Post
class to create 1-to-n relationships between different custom post types.
In an plugin I'm developing, I added a SelectControl
to the PluginDocumentSettingPanel
to be able to set the id of a related custom post type in another custom post type. According to the docs, I should use getEditedPostAttribute
to retrieve the current "parent" (the id of the related custom post type) of the custom post type being edited:
select( 'core/editor' ).getEditedPostAttribute( 'parent' )
But, for some reason, I'm getting an undefined
value even though the post_parent
property is set. A custom meta box in the classic editor shows it correctly.
If I set the hierarchical
property in the custom post type to true
, the selector works and I get the correct value. Is this by design? Or are we forced to use a custom meta field to store this data, now?
It's a common practice to use the post_parent
property in the WP_Post
class to create 1-to-n relationships between different custom post types.
In an plugin I'm developing, I added a SelectControl
to the PluginDocumentSettingPanel
to be able to set the id of a related custom post type in another custom post type. According to the docs, I should use getEditedPostAttribute
to retrieve the current "parent" (the id of the related custom post type) of the custom post type being edited:
select( 'core/editor' ).getEditedPostAttribute( 'parent' )
But, for some reason, I'm getting an undefined
value even though the post_parent
property is set. A custom meta box in the classic editor shows it correctly.
If I set the hierarchical
property in the custom post type to true
, the selector works and I get the correct value. Is this by design? Or are we forced to use a custom meta field to store this data, now?
1 Answer
Reset to default 1You 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.
本文标签: javascriptHow to get the parent of a nonhierarchical custom post type being edited in Gutenberg
版权声明:本文标题:javascript - How to get the parent of a non-hierarchical custom post type being edited in Gutenberg 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742295026a2448550.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论