在我为Gutenberg构建的插件中,我注册了一个名为_related_posts
, 其中存储了一组帖子ID:
PHP:
function register_meta_fields() {
register_post_meta( \'\', \'_related_posts\', array(
\'show_in_rest\' => true,
\'single\' => true,
\'type\' => \'array\',
\'show_in_rest\' => array(
\'schema\' => array(
\'type\' => \'array\',
\'items\' => array(
\'type\' => \'integer\',
),
),
),
\'auth_callback\' => function() {
return current_user_can( \'edit_posts\' );
}
) );
}
我正在使用useEntityProp
钩子在JS中获取和设置此元字段:JS:
const [ meta, setMeta ] = useEntityProp(
\'postType\',
\'post\',
\'meta\'
);
我有一个onUpdateRelatedPosts()
当用户从列表中选择帖子时调用的函数:JS:
function onUpdateRelatedPosts( post ) {
setMeta( { ...meta, \'_related_posts\': [post.id] } );
}
此函数将post对象作为参数,并将其id存储在_related_posts
meta字段,但我无法让它按我想要的方式工作。它不会在数组末尾附加id,而是用添加的id替换整个元字段。我做错了什么?