admin管理员组文章数量:1277297
I have successfully created a custom Gutenberg block which can modify several meta data and native post data. Among other, I've built a dropdown with all the terms of a custom taxonomy (with show_in_rest
set to TRUE
). The dropdown works well, but how can I save the selection back to the post?
<SelectControl
label="My Dropdown"
options={ options }
value={ value }
onChange={ v => update( v ) }
/>
Whenever I select another value in the dropdown, the following function is executed:
const update = ( t ) => {
// The selected option can have a value of zero (= assign no term)
const ids = [];
if ( t.value ) {
ids.push( t.value );
}
// the following line does not work
// editEntityRecord( 'postType', 'my_custom_post_type', post.id, { 'my_custom_taxonomy': [ t.value ] } );
// nor does this line
editPost( {
my_custom_taxonomy: [ t.value ]
});
// ... updating value in the state in order to make the dropdown work
}
I am not even sure if I should use editPost
or editEntityRecord
.
How can I save the new selected term id to the post?
I have successfully created a custom Gutenberg block which can modify several meta data and native post data. Among other, I've built a dropdown with all the terms of a custom taxonomy (with show_in_rest
set to TRUE
). The dropdown works well, but how can I save the selection back to the post?
<SelectControl
label="My Dropdown"
options={ options }
value={ value }
onChange={ v => update( v ) }
/>
Whenever I select another value in the dropdown, the following function is executed:
const update = ( t ) => {
// The selected option can have a value of zero (= assign no term)
const ids = [];
if ( t.value ) {
ids.push( t.value );
}
// the following line does not work
// editEntityRecord( 'postType', 'my_custom_post_type', post.id, { 'my_custom_taxonomy': [ t.value ] } );
// nor does this line
editPost( {
my_custom_taxonomy: [ t.value ]
});
// ... updating value in the state in order to make the dropdown work
}
I am not even sure if I should use editPost
or editEntityRecord
.
How can I save the new selected term id to the post?
Share Improve this question asked Nov 23, 2021 at 16:12 urukuruk 6348 silver badges22 bronze badges1 Answer
Reset to default 1I found it (Argh!).
I was nearly there, but I made a stupid mistake. Inside the update
-function, the argument t
is not an object, but the value itself:
const update = ( t ) => {
// t is the selected term's ID as a string. Transform it to an integer
const x = parseInt( t, 10 );
// Update the assigned term or remove it, when zero
editEntityRecord( 'postType', 'my_custom_post_type', post.id, {
my_custom_taxonomy: x ? [ x ] : []
} );
// ...
}
本文标签: block editorGutenberg How to update associated Terms of Custom Taxonomy to Current Post
版权声明:本文标题:block editor - Gutenberg: How to update associated Terms of Custom Taxonomy to Current Post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741209583a2358840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论