In the WordPress website, I have added one custom post type "inventory".To post data to that inventory created one end rest API endpoint. the created endpoint is not getting all the metadata from the inventory post type. In that inventory post type, I have an input field to enter date but while getting the data using endpoint I am not getting that date field data. I have tried register_rest_field() to get metadata using API endpoint, but in postman, I am getting only some fields.
My HTML Code looks like below
<div class="from_date inventory-form-to-input">
<input type="text" style="border: 1px solid #ddd;" class="pac-date-picker hasDatepicker"
name="nets_availability_pickup_datetime[]" value="0000-00-00 00:00:00" autocomplete="off"
id="dp1605714040849">
</div>
<div class="to_date inventory-form-to-input">
<input type="text" style="border: 1px solid #ddd;" class="pac-date-picker hasDatepicker"
name="nets_availability_dropoff_datetime[]" value="0000-00-00 00:00:00" autocomplete="off"
id="dp1605714040850">
</div>
And response in postman:
{
"id": 197794,
"date": "2020-11-03T04:33:35",
"date_gmt": "2020-11-03T10:33:35",
"guid": {
"rendered": "https://mywebsite.com.com/?post_type=inventory&p=197794"
},
"modified": "2020-11-17T10:11:24",
"modified_gmt": "2020-11-17T16:11:24",
"slug": "000-030-065x-001",
"status": "publish",
"type": "inventory",
"link": "https://mywebsite.com.com/inventory/000-030-065x-001/",
"title": {
"rendered": "000-030-065x-001"
},
"featured_media": 0,
"parent": 0,
"template": "",
"meta": {
"nets_hourly_ranges_cost": [
"a:0:{}"
],
"nets_daily_pricing": [
"a:0:{}"
],
"nets_monthly_pricing": [
"a:0:{}"
],
"nets_day_ranges_cost": [
"a:0:{}"
],
"_edit_lock": [
"1605629551:619"
],
"_edit_last": [
"619"
],
"quantity": [
"1"
],
"pricing_type": [
"general_pricing"
],
"general_price": [
"100"
],
"_yoast_wpseo_focuskeywords": [
"[]"
],
"_yoast_wpseo_keywordsynonyms": [
"[\"\"]"
]
},
"quantity": "1",
"pricing_type": "general_pricing",
"general_price": "100",
"nets_availability_block_by": [],
"nets_availability_pickup_datetime": "",
"_links": {
"self": [
{
"href": "https://mywebsite.com.com/wp-json/wp/v2/inventory/197794"
}
],
"collection": [
{
"href": "https://mywebsite.com/wp-json/wp/v2/inventory"
}
],
"about": [
{
"href": "https://mywebsite.com/wp-json/wp/v2/types/inventory"
}
],
"wp:attachment": [
{
"href": "https://mywebsite.com/wp-json/wp/v2/media?parent=197794"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
tried below code to create a custom endpoint and to get metadata:
function my_plugin_rest_route_for_post( $route, $post ) {
if ( $post->post_type === 'inventory' ) {
$route = '/wp/v2/inventory/' . $post->ID;
}
return $route;
}
add_action( 'rest_api_init', function () {
register_rest_field( 'inventory', 'quantity', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], 'quantity', true );
},
) );
register_rest_field( 'inventory', 'pricing_type', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], 'pricing_type', true );
},
) );
register_rest_field( 'inventory', 'general_price', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], 'general_price', true );
},
) );
register_rest_field( 'inventory', 'nets_availability_block_by', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], 'nets_availability_block_by',false);
},
) );
register_rest_field( 'inventory', 'nets_availability_pickup_datetime', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], 'nets_availability_pickup_datetime', true );
},
) );
/*used to get all metadata*/
register_rest_field( 'inventory', 'meta', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], '', true );
},
) );
} );
please anyone suggest me how to get those datefields 'nets_availability_pickup_datetime' & 'nets_availability_dropoff_datetime'. Thanks in advance.