[英]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;
}
什么是“類型”模式關鍵字,我該如何解決?
仍在尋找解決方案... 8 月 14 日。
當rest_validate_value_from_schema()
時沒有必需的參數或使用不正確的值時,WP 5.5 開始發出通知。 type
屬性的允許值為'array', 'object', 'string', 'number', 'integer', 'boolean', 'null'
。
您共享的代碼對我沒有產生任何錯誤,因此問題可能出在您未共享的某些代碼中? 如果您要注冊一個帶有屬性的塊,那么每個屬性都需要一個type
參數。
另外:有一個專用的 WordPress Stack Exchange 。 將來你可能會在那里得到更好的回應。
試試這個 - 停用所有插件並刷新,看看它是否有效。 如果是,則逐個安裝插件並在每次插件安裝后刷新。 如果您找到導致問題的插件,請將其刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.