您需要在调用editPost后调用savePost。参考来源的处理方式:https://github.com/WordPress/gutenberg/blob/trunk/packages/editor/src/components/post-visibility/index.js 它显示在更改可见性后立即调用savePost。
在实践中:
import { PluginPostStatusInfo } from \'@wordpress/edit-post\';
import { __ } from \'@wordpress/i18n\';
import { registerPlugin } from \'@wordpress/plugins\';
import { ToggleControl } from \'@wordpress/components\';
import { useSelect, useDispatch } from \'@wordpress/data\';
function PostChangeToggle() {
const status = useSelect( ( select ) => {
return select( \'core/editor\' ).getEditedPostAttribute( \'status\' );
}, [] );
const { editPost, savePost } = useDispatch( \'core/editor\' );
return (
<PluginPostStatusInfo
name="prefix-post-change"
title={ __( \'Post Change\', \'text-domain\' ) }
className="prefix-post-change"
initialOpen={ true }
>
<ToggleControl
label={ __( \'Draft\', \'text-domain\' ) }
checked={ status === \'draft\' }
onChange={ () => {
editPost( {
status: status === \'draft\' ? \'publish\' : \'draft\',
} );
savePost();
} }
/>
</PluginPostStatusInfo>
);
}
registerPlugin( \'prefix-post-change\', {
render: PostChangeToggle,
} );
上面的代码将从草稿和发布创建一个简单的来回切换。您可以调出“草稿”或其他状态或自定义状态。我有一个;“过期”;我以类似方式控制的状态。