如何修复 WordPress 5.5 中的 Rest API 错误?

How to fix Rest API error in WordPress 5.5?

我刚刚更新到 WordPress 5.5,在调试模式下出现以下错误。

rest_validate_value_from_schema was called incorrectly. The "type" schema keyword for [0] can only be one of the built-in types: array, object, string, number, integer, boolean, and null. Please see Debugging in WordPress for more information. 

我正在使用自定义端点来创建一些自定义块。

function example_custom_route()
{
register_rest_route('example/v1', 'posts', [
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'example_result_posts',
    'permission_callback' => function () {
        return current_user_can( 'edit_posts' );
    }
]);
}
add_action('rest_api_init', 'example_custom_route');

function example_result_posts()
{
    $posts_result = [];
    $post_type = 'post';

    $posts = get_posts([
        'post_type' => $post_type,
        'order' => 'DESC',
        'orderby' => 'date',
        'ignore_sticky_posts' => 1,
        'posts_per_page' => -1,
        'post_status' => 'publish'
    ]);
    if (!empty($posts)) {
        foreach ($posts as $post) {
            array_push($posts_result, [
                'value' => $post->ID,
                'label' => wp_specialchars_decode(wp_strip_all_tags($post->post_title)),
            ]);
        }
        wp_reset_postdata();
    }
    return $posts_result;
}

什么是 'The "type" schema keyword',我该如何解决?

8 月 14 日,仍在寻找这个...的解决方案

试试这个 - 停用所有插件并刷新,看看它是否有效。如果是,则一个一个安装插件,每次安装插件后刷新。如果您找到导致问题的插件,请将其删除。

WP 5.5 started throwing notices when rest_validate_value_from_schema() is called without required parameters, or with incorrect valuestype 属性 的允许值为 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null'.

你分享的代码对我来说没有产生任何错误,所以问题可能出在你没有分享的部分代码中?如果您要注册具有属性的块,则 each attribute will need a type parameter.

旁白:有 a dedicated WordPress Stack Exchange。以后你可能会在那里得到更好的回应。