Wordpress REST API читает сообщения мета, но не обновляет - PullRequest
0 голосов
/ 08 ноября 2018

Я пытаюсь создать PowerApp, в котором я хочу интегрировать мета поля поста. Я вставил этот код в файл functions.php. Но я могу читать и записывать данные для полей WordPress по умолчанию, но я не могу обновить настраиваемые поля записей.

add_action( 'rest_api_init', 'create_api_posts_meta_field' );

function create_api_posts_meta_field() {

    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'post', 'post-meta-fields', array(
           'get_callback'    => 'get_post_meta_for_api',
           'update_callback' => 'get_post_meta_update_for_api',
           'schema'          => null,
        )
    );
}

function get_post_meta_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];

    //return the post meta
    return get_post_meta( $post_id );
}

function get_post_meta_update_for_api( $value, $object, $field_name ) {
    return update_post_meta( $object[ 'id' ], $field_name, $value );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...