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?

    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?

1 Answer 1


I 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 ] : [] 
    } );
    // ... 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.