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 badges
Add a comment  | 

1 Answer 1

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

本文标签: block editorGutenberg How to update associated Terms of Custom Taxonomy to Current Post