Hi, I want to create a new API Route for Coupon, in which were added many custom fields from Metabox
Could I query all the MB field values without listing all the field keys per post metadata
Here is the working code that I create for the new route, I get the post meta value stored in Custom Table by the post_id.
function get_custom_post_by_id_callback( $data ) {
$post_id = $data['id'];
$post = get_post( $post_id );
$args = [
'storage_type' => 'custom_table',
'table' => 'wp_personal_coupons',
];
$post_meta = rwmb_meta( 'voucher_entry', $args, $post_id );
$response = array(
'id' => $post->ID,
'title' => $post->post_title,
'content' => $post->post_content,
'post_meta' => $post_meta
);
return $response;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'metabox', '/coupons/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_custom_post_by_id_callback',
));
});
Thanks.