Добавление полей к ресурсам
function register_post_custom_field() { register_api_field( 'post', 'custom_field', array( 'get_callback' => 'get_custom_field', 'update_callback' => null, 'schema' => null, ) ); }
function get_custom_field( $object, $field_name, $request ) { return get_post_meta( $object[ 'id' ], $field_name, true ); }
Добавление конечных точек
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
'methods' => 'GET',
'callback' => 'get_post_title_by_author'
) );
} );
Ниже приведена функция обратного вызова, которая вызывается при доступе к конечной точке GET через API WP. К вашему сведению, сообщения извлекаются по идентификатору автора сообщения в этой функции, и возвращается последний заголовок сообщения.
function get_post_title_by_author( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}