Hi I'm building up a WordPress rest API's custom endpoint on my project. I'm using the following code to get the single response from the website.
function get_single_post( $request ) {
$response = array();
$args = [
'id' => $request['id'],
'post_type' => 'payment',
];
$post = get_posts($args);
$metas = get_post_meta($request['id'],'');
$post_id = $post->ID;
$response[$post_id]['response'] = $post[0];
$response[$post_id]['metas'] = $metas;
return $response;
}
add_action('rest_api_init', function(){
register_rest_route('booking/v1', 'posts',[
'methods'=> 'GET',
'callback' => 'booking_posts',
]);
register_rest_route('booking/v1','/posts/(?P<id>\d+)',[
'methods' => 'GET',
// Register the callback for the endpoint.
'callback' => 'get_single_post',
]);
});
I'm not getting the post with the posts/ in my response. Kindly advise what I'm doing wrong here?
Here's my response:
{
"": {
"response": {
"ID": 12042,
"post_author": "10",
"post_date": "2020-06-12 15:15:42",
"post_date_gmt": "2020-06-12 14:15:42",
"post_content": "",
"post_title": "xxxxxxxxxxxxxxx",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "xxxxxxxxxxx",
"to_ping": "",
"pinged": "",
"post_modified": "2020-06-12 15:15:42",
"post_modified_gmt": "2020-06-12 14:15:42",
"post_content_filtered": "",
"post_parent": 0,
"guid": "https://xxxxxxxx/payment/xxxxxxxxxx/",
"menu_order": 0,
"post_type": "payment",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},
"metas": {
"chauffeur_payment_status": [
"Unpaid"
],
"chauffeur_payment_num_passengers": [
"1"
],
"chauffeur_payment_num_bags": [
"0"
],
"chauffeur_payment_first_name": [
"xxxxxxx"
],
"chauffeur_payment_last_name": [
"xxxxxxxx"
],
"chauffeur_payment_email": [
"xxxxxxxxx"
],
"chauffeur_payment_phone_num": [
"xxxxxx"
],
"chauffeur_payment_flight_number": [
""
],
"chauffeur_payment_additional_info": [
""
],
"chauffeur_payment_pickup_address": [
"xxxxxxx"
],
"chauffeur_payment_dropoff_address": [
"xxxxxxxx"
],
"chauffeur_payment_pickup_date": [
"12/06/2020"
],
"chauffeur_payment_pickup_time": [
"12:00"
],
"chauffeur_payment_trip_distance": [
"3,907 km"
],
"chauffeur_payment_trip_time": [
"1 day 12 hours"
],
"chauffeur_payment_item_name": [
"Standard-size taxi"
],
"chauffeur_payment_trip_type": [
"one_way"
],
"chauffeur_payment_return_journey": [
"One Way"
],
"chauffeur_payment_num_hours": [
""
],
"chauffeur_payment_amount": [
"xxxx"
],
"chauffeur_payment_full_pickup_address": [
""
],
"chauffeur_payment_pickup_instructions": [
""
],
"chauffeur_payment_full_dropoff_address": [
""
],
"chauffeur_payment_dropoff_instructions": [
""
]
}
}
}
The id of response->ID should be same as posts/. It's not.
show_in_rest
when registering the post type instead and letting WP Core build an endpoint for you?