Support Forum
Support › MB REST API › Add Metabox Post Meta to Custom API EndpointResolved
Hi
I have been looking around and cannot find the answer. I have created my own endpoint and want to return all of the available metabox post meta without specifying each ID (but dont mind doing so if thats the only way to do it). So essentially return all of the metabox key and values and add them to my custom endpoint.
my fields
public function createProductFields() {
add_filter( 'rwmb_meta_boxes', 'dc_product_register_meta_boxes' );
function dc_product_register_meta_boxes( $meta_boxes ) {
global $wpdb;
$currentDate = date('Y-m-d');
$currentTime = date('H:i');
$meta_boxes[] = [
'title' => esc_html__( 'Products', 'my-translation-table' ),
'post_types' => 'product', //target cpt
'storage_type' => 'custom_table', // //store in custom table not default meta table
'table' => $wpdb->prefix . 'dc_products', // custom table name
'id' => 'product',
'context' => 'normal',
'fields' => [
[
'type' => 'text',
'name' => esc_html__( 'Type', 'my-translation-table' ), //todo: look at adding in translation file and referencing it with e.g. 'my-translation-table'
'id' => 'type', //mysql name
'desc' => esc_html__( '', 'my-translation-table' ),
'class' => 'dc-type',
//'required' => true,
],
[
'type' => 'datetime',
'name' => esc_html__( 'Start Date', 'my-translation-table' ),
'id' => 'start_date', //mysql name
'js_options' => array(
'dateFormat' => 'dd-mm-yy',
'minDate' => '$currentDate',
'defaultTime' => '$currentTime',
'timeFormat' => "HH:mm",
'showTimepicker' => true,
),
'save_format' => 'Y-m-d H:i:s',
'class' => 'dc-start-date',
//'required' => true,
],
[
'type' => 'datetime',
'name' => esc_html__( 'End Date', 'my-translation-table' ),
'id' => 'end_date', //mysql name
'js_options' => array(
'dateFormat' => 'dd-mm-yy',
'minDate' => '$currentDate',
'timeFormat' => "HH:mm",
'showTimepicker' => true,
),
'save_format' => 'Y-m-d H:i:s',
'class' => 'dc-end-date',
//'required' => true,
],
[
'type' => 'number',
'name' => esc_html__( 'Price', 'my-translation-table' ),
'id' => 'price', //mysql name
'step' => 'any',
'class' => 'dc-price',
//'required' => true,
],
[
'type' => 'text',
'name' => esc_html__( 'Condition', 'my-translation-table' ),
'id' => 'condition', //mysql name
'class' => 'dc-condition',
//'required' => true,
],
[
'type' => 'text',
'name' => esc_html__( 'Location', 'my-translation-table' ),
'id' => 'location', //mysql name
'class' => 'dc-location',
//'required' => true,
],
[
'type' => 'imagepreview',
'name' => 'image',
'id' => 'images', //mysql name
//'required' => true,
],
],
];
return $meta_boxes;
}
}
my custom endpoint
public function createProductRestApiEndpoint() {
add_action('rest_api_init', function () {
register_rest_route( 'dripcreate/v2', 'products', array(
'methods' => 'GET',
'callback' => ['Products', 'get_products'] //class & function name call
));
});
}
public function get_products() {
$args = array(
'post_type' => 'product'
);
$posts = get_posts($args);
if (empty($posts)) {
//do something
}
$response = new WP_REST_Response($posts);
$response->set_status(200);
return $response;
}
Any advise on the best approach would be great!
Thanks
Hi,
You can follow this article to add post meta to the custom route REST API https://blog.abmsourav.com/wordpress-meta-in-rest-api/
For the post meta stored in the custom table, you can follow this topic https://wordpress.stackexchange.com/questions/221808/how-would-i-add-custom-tables-endpoints-to-the-wp-rest-api
Refer to the documentation https://developer.wordpress.org/reference/functions/register_rest_field/
Hi
Ok thanks for the links, it worked perfectly.
Nick