I’m trying to use “Show in REST API” but it’s not working.
functions.php
function my_awesome_func($data)
{
$posts = get_posts(array(
'author' => $data['id'],
));
if (empty($posts)) {
return null;
}
return $posts
;
}
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
));
});
endpoint
http://localhost/rest/wp-json/myplugin/v1/author/1
response
[
{
"ID": 1,
"post_author": "1",
"post_date": "2022-06-30 11:24:38",
"post_date_gmt": "2022-06-30 14:24:38",
"post_content": "<!-- wp:paragraph -->\nBoas-vindas ao WordPress. Esse é o seu primeiro post. Edite-o ou exclua-o, e então comece a escrever!
\n<!-- /wp:paragraph -->",
"post_title": "Olá, mundo!",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "ola-mundo",
"to_ping": "",
"pinged": "",
"post_modified": "2022-06-30 11:31:24",
"post_modified_gmt": "2022-06-30 14:31:24",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/rest/?p=1",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "1",
"filter": "raw"
}
]
Can somebody help me?
Thanks.