0
votes

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&#038;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.

1
I would like to know if you are already able to retrieve the date by using route of 'nets_availability_pickup_datetime' and 'nets_availability_pickup_datetime'Fabrice Fabiyi
No I am not able retrieve nets_availability_pickup_datetime & nets_availability_dropoff_datetime.Actually 197794 for this id i have both dates but not getting those.Devsandy
Please show your code where you saved nets_availability_pickup_datetime & nets_availability_dropoff_datetime in meta.Fabrice Fabiyi
Can you please help me to create endpoint for custom table wordpressDevsandy
i can't help you if not show me your code otherwise i can recommend you these lnks Add custom endpoint get data from custom tableFabrice Fabiyi

1 Answers

0
votes

You can get inspiration from one of my old code. Permission callback is used to add a security layer to your API, but is usually optional. The parameter args is also optional.

register_rest_route('v2/boutique', '/order/status', array(
        'methods' => 'POST',
        'permission_callback' => array($this, 'check_access'),
        'callback' => array($this, "api_check_order"),
        'args' => [
            'transaction_id', 'order_id'
        ],

    ));




public function check_access(WP_REST_Request $request)
{
    // Check credential 
   // if(in_array($_SERVER['REMOTE_ADDR'], self::$available_ip)){
    $creds  = $request->get_header('token');

    if (!empty($creds)) {
        if (self::verify_token(self::$creds_pass, $creds)) {
            return true;
        } else {
            return new WP_Error(
                'rest_forbidden',
                __('Error'),
                array('status' => 404)
            );
        }
    } else {
        return new WP_Error(
            'rest_forbidden',
            __('Error'),
            array('status' => 404)
        );
    }

}