questions

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

Rate this post

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:

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

Leave a Reply

Your email address will not be published.

Back to top button