Set comment meta REST API WordPress

Viewed 251

i create comment in WordPress with REST API. But can't set comment meta. I don't know why, maybe i missing a little bit. So please help.

The code:

// Data
var ajax_data = {
    author: user.user_id,
    author_email: user.user_email,
    content: comment,
    parent: parent,
    post: post_id,
    meta: {
        '_crating_speed': rating_speed,
        '_crating_price': rating_price,
    },
}

// Set header
$.ajaxSetup({
    headers: {
        'Authorization': "Bearer " + user.token,
    }
});

// AJAX
$.post( rest_base + '/comments', ajax_data ).done( function( response ) {  
    console.log(response);
}).fail( function( xhr, status, error ) {
    console.log( error);
}, 'json' );

The meta _crating_speed and _crating_price not inserted to database.

1 Answers

Same issue, solution is to add these using register_meta() which will add the meta fields to the schema of the comment.

https://developer.wordpress.org/reference/functions/register_meta/

In my case the following worked:

add_action('init', function() {
    register_meta('comment', 'stars', [
        'type' => 'number',
        'description' => __('Stars'),
        'single' => true,
        'show_in_rest' => true
    ]);
}, 10 , 0);
Related