I've got the following basic route for my custom REST endpoint:
add_action('rest_api_init', 'my_cpt_route');
function my_cpt_route() {
register_rest_route('mytheme/v1/', 'my_cpt', array(
'methods' => WP_REST_SERVER::READABLE,
'callback' => 'my_cpt_results',
));
}
And this is the CB function:
function my_cpt_results() {
$args = array(
'post_type' => array('my_cpt'),
'posts_per_page' => -1,
);
$mainQuery = new WP_Query($args);
$results = array();
while ($mainQuery->have_posts()) {
$mainQuery->the_post();
array_push($results, array(
'id' => get_the_ID(),
'title' => get_the_title(),
'url' => get_the_permalink(),
'image' => array(
'small' => get_the_post_thumbnail_url(0, 'thumbnail'),
'medium' => get_the_post_thumbnail_url(0, 'medium'),
'large' => get_the_post_thumbnail_url(0, 'large'),
)
));
$results = array_values(array_unique($results, SORT_REGULAR));
}
wp_reset_postdata();
return $results;
}
This works just fine when I retrieve all the posts (so if I go to "http://mysite.test/wp-json/mytheme/v1/my_cpt/" I get all the CPT posts).
What I expected is that if I append an id of a published CPT post at the end of the URL (eg.: "http://mysite.test/wp-json/mytheme/v1/my_cpt/56") I had to get only the post with id 56. Instead, I get a 404 "rest_no_route".
Maybe I need to modify the register_rest_route args? (but how?)...