WordPress/Gutenberg : 运行 时发生错误 'mapSelect': 无法读取 属性

WordPress/Gutenberg : An error occurred while running 'mapSelect': Cannot read property

我创建了 4 个自定义 post 类型:'dissertation''subject-free''subject-imposed''curriculum-vitae'

我创建了一个要在 3 种自定义 post 类型上显示的元数据框:'dissertation''subject-free''subject-imposed'

当我想在 'curriculum-vitae' 上创建一个 post 时。我收到一个错误: 错误:运行 'mapSelect' 时发生错误:无法读取未定义的 属性 '_metafield_presentation'

import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { TextareaControl, Toolbar, ToolbarButton } from '@wordpress/components';
import { formatBold, formatItalic, link } from '@wordpress/icons';

const TextController = (props) => {
    const meta = useSelect(
        (select) => select('core/editor').getEditedPostAttribute('meta')['_metafield_presentation']
    );
    const { editPost } = useDispatch('core/editor');
    return (
        <TextareaControl
        label={__("Ajoutez ici votre texte de présentation permettant de décrire votre sujet libre. Vous pouvez vous inspirer de votre note d'intention en faisant un copier-coller. La limite est de 800 caractères maximum.", "textdomain")}
        value={meta}
        onChange={(value) => editPost({ meta: { _metafield_presentation: value } })}
        />
    );
};

const PluginDocumentSettingPanelDemo = () => (
    <PluginDocumentSettingPanel name="text-presentation" title="Texte de présentation" className="custom-panel-presentation">
    <TextController />
    </PluginDocumentSettingPanel>
);

registerPlugin('plugin-document-setting-panel-presentation', {
    render: PluginDocumentSettingPanelDemo,
    icon: null
});

并且在 functions.php 中:

function ccn_register_metas() {
    $post_types = [
        'dissertation',
        'subject-free',
        'subject-imposed'
    ];

    foreach ( $post_types as $post_type ) {

        register_post_meta(
            $post_type,
            '_metafield_presentation',
            array(
                'show_in_rest' => true,
                'type' => 'string',
                'single' => true,
                'sanitize_callback' => 'sanitize_text_field',
                'auth_callback' => function() {
                    return current_user_can( 'edit_posts' );
                },
            )
        );

    }

}

add_action('init', 'ccn_register_metas');

请问有什么问题吗?

post 元尚未在 curriculum-vitae post 类型上注册,因此 WordPress 无法更新它。 WordPress 正在尝试更新它,因为 PluginDocumentSettingPanel 仍在 curriculum-vitae post 类型上呈现。

我通常会在使用任何自定义 post 元或为该 post 类型添加任何 PluginDocumentSettingPanel 之前检查 post 类型:

const postType = useSelect( ( select ) => {
    return select( 'core/editor' ).getCurrentPostType();
} );

if ( postType === 'your-post-type' ) {
    // Do something with the post meta here
}

一个更完整的例子:

import { __ } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { useSelect } from '@wordpress/data';
import { TextControl } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';

const MyPlugin = () => {
    const postType = useSelect( ( select ) => {
        return select( 'core/editor' ).getCurrentPostType();
    } );

    const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

    if ( postType !== 'my-post-type' ) {
        // Not the correct post type, so we don't need to render this document setting panel
        return null;
    }

    return (
        <PluginDocumentSettingPanel
            name="my-plugin"
            title={ __( 'Plugin Title', 'text-domain' ) }
        >
            <TextControl
                label={ __( 'Text Field', 'text-domain' ) }
                value={ meta._meta_name ? meta._meta_name : '' }
                onChange={ ( value ) => setMeta( { _meta_name: value } ) }
            />
        </PluginDocumentSettingPanel>
    );
};

registerPlugin( 'my-plugin', {
    render: MyPlugin,
    icon: '',
} );