I'm currently making a custom API endpoint for a site that I'm building, which works absolutely fine aside from this issue with author ID's.
If I do a GET request to website.local/wp-json/wp/v2/podcasts
it returns valid JSON of all of the podcasts Custom Post Type. If I do a GET request to website.local/wp-json/wp/v2/podcasts?author=1
it also returns valid JSON of all of the podcasts Custom Post Type - no problems here.
The problem however comes from some other code that I'm writing, I'm trying to do this in a custom function:
$server = rest_get_server();
$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts?author=1');
$podcastsResponse = rest_do_request( $podcastsRequest );
$podcastsData = $server->response_to_data( $podcastsResponse, false );
When I run it in Postman I get the following response:
{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}
If I do this call internally inside the function, it works fine:
$server = rest_get_server();
$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts');
$podcastsResponse = rest_do_request( $podcastsRequest );
$podcastsData = $server->response_to_data( $podcastsResponse, false );
I cannot work out why it's producing that error when a direct call to it works fine?
EDIT: To give more context, this is the function I'm using:
function get_all_posts(WP_REST_Request $request){
$server = rest_get_server();
if($request->get_param('id')){
// $author = $request->get_param('author');
$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts?author=1');
// $articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles?author=1');
// $webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars?author=1');
// $expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews?author=1');
// $guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides?author=1');
} else {
$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts' );
$articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles' );
$webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars' );
$expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews' );
$guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides' );
}
$podcastsResponse = rest_do_request( $podcastsRequest );
$podcastsData = $server->response_to_data( $podcastsResponse, false );
$articlesResponse = rest_do_request( $articlesRequest );
$articlesData = $server->response_to_data( $articlesResponse, false );
$webinarsResponse = rest_do_request( $webinarsRequest );
$webinarsData = $server->response_to_data( $webinarsResponse, false );
$expertInterviewsResponse = rest_do_request( $expertInterviewsRequest );
$expertInterviewsData = $server->response_to_data( $expertInterviewsResponse, false );
$guidesResponse = rest_do_request( $guidesRequest );
$guidesData = $server->response_to_data( $guidesResponse, false );
$merged = array_merge(
$podcastsData,
$articlesData,
$webinarsData,
$expertInterviewsData,
$guidesData,
);
return $merged;
}
add_action('rest_api_init', function () {
register_rest_route( 'wp/v2', '/all-posts', [
'methods' => 'GET',
'callback' => 'get_all_posts',
]);
});
As I've said above, if I just use the new WP_REST_Request( 'GET', '/wp/v2/podcasts' );
endpoints, it works fine, but if I try and use new WP_REST_Request( 'GET', '/wp/v2/podcasts?author=1' );
it returns that error, despite a direct call to website.local/wp-json/wp/v2/podcasts?author=1
returning all the posts for the author.
/all-posts
which returns every post type as one array. As I said in the question, the code works when visiting the endpoints in postman, but doing/wp/v2/podcasts?author=1
instead of/wp/v2/podcasts
insideWP_REST_Request
completely kills the request. Going to the browser won't fix this issueWP_REST_Request
or similar??author=1
in the request, I had to change it to$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts'); $podcastsRequest->set_query_params( array( 'author' => $author ));