Search code examples
wordpresswordpress-themingwordpress-rest-api

How to register_meta type of array and add it to Wordpress REST API properly?


I have an issue with register_meta(),

I'm trying to create meta of an array for my custom post type and add it to REST API, but nothing, here is my code:

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');

And I can't see this meta in REST API. If I change meta type to string or boolean or number it will be shown.

What am I doing wrong?

Thanks in advance.


Solution

  • I found answer here. So solution is:

    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' );
        }
    ) );