如何 register_meta 类型的数组并将其正确添加到 Wordpress REST API?
How to register_meta type of array and add it to Wordpress REST API properly?
我对 register_meta()
、
有疑问
我正在尝试为我的自定义 post 类型创建一个数组的元数据并将其添加到 REST API,但是什么也没有,
这是我的代码:
function thefuturmeta_register_portfolio_meta() {
register_meta( 'post', '_thefuturmeta_port_add_gallery', array(
'object_subtype' => '_themename_portfolio',
'show_in_rest' => true,
'type' => 'array',
'single' => true,
'sanitize_callback' => '',
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
}
add_action('init', 'thefuturmeta_register_portfolio_meta');
我在 REST 中看不到这个元数据 API。如果我将元类型更改为字符串或布尔值或数字,它将被显示。
我做错了什么?
提前致谢。
我找到了答案 here。
所以解决方案是:
register_meta( 'post', '_thefuturmeta_port_add_gallery', array(
'object_subtype' => '_themename_portfolio',
'single' => true,
'sanitize_callback' => '',
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'number',
),
),
),
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
我对 register_meta()
、
我正在尝试为我的自定义 post 类型创建一个数组的元数据并将其添加到 REST API,但是什么也没有, 这是我的代码:
function thefuturmeta_register_portfolio_meta() {
register_meta( 'post', '_thefuturmeta_port_add_gallery', array(
'object_subtype' => '_themename_portfolio',
'show_in_rest' => true,
'type' => 'array',
'single' => true,
'sanitize_callback' => '',
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
}
add_action('init', 'thefuturmeta_register_portfolio_meta');
我在 REST 中看不到这个元数据 API。如果我将元类型更改为字符串或布尔值或数字,它将被显示。
我做错了什么?
提前致谢。
我找到了答案 here。 所以解决方案是:
register_meta( 'post', '_thefuturmeta_port_add_gallery', array(
'object_subtype' => '_themename_portfolio',
'single' => true,
'sanitize_callback' => '',
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'number',
),
),
),
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );