Hi! I am working on a plugin that creates a custom post type and some custom fields.
A couple of the fields are dates.
I tried using the tag filters to format the date, i.e., {cf_ewr_event_start_date:F j, Y}
, but to no avail.
By default it is printing out like this:
YYYY-MM-DD
(e.g., 2024-06-11), which is a fairly good starting point for a typical date format, but perhaps incomplete?
Here is my code:
// Register meta fields with REST API
function erw_register_meta_fields() {
$meta_keys = array(
‘ewr_event_start_date’ => ‘Event Start Date’,
‘ewr_event_end_date’ => ‘Event End Date’,
‘ewr_event_start_time’ => ‘Event Start Time’,
‘ewr_event_end_time’ => ‘Event End Time’,
‘ewr_ticket_price’ => ‘Ticket Price’,
‘ewr_max_ticket_quantity’ => ‘Max Ticket Quantity’,
‘ewr_ticket_addon’ => ‘Ticket Add-on’
);
foreach ($meta_keys as $meta_key => $description) {
register_post_meta('event', $meta_key, array(
'type' => 'string',
'description' => $description,
'single' => true,
'show_in_rest' => array(
'schema' => array(
'type' => 'string',
'context' => array('view', 'edit', 'embed'),
),
),
'auth_callback' => function() {
return current_user_can('edit_posts');
}
));
}
}
add_action(‘init’, ‘erw_register_meta_fields’);
// Expose custom fields in REST API response
function erw_expose_custom_fields_in_rest($response, $post, $request) {
if ($post->post_type === ‘event’) {
$meta_fields = array(
‘ewr_event_start_date’,
‘ewr_event_end_date’,
‘ewr_event_start_time’,
‘ewr_event_end_time’,
‘ewr_ticket_price’,
‘ewr_max_ticket_quantity’,
‘ewr_ticket_addon’
);
foreach ($meta_fields as $meta_field) {
$meta_value = get_post_meta($post->ID, $meta_field, true);
if (!empty($meta_value)) {
$response->data['meta'][$meta_field] = $meta_value;
}
}
}
return $response;
}
add_filter(‘rest_prepare_event’, ‘erw_expose_custom_fields_in_rest’, 99, 3);
I am curious to know if there is a way I can format or indicate that this field should be able to be formatted as a date by Bricks, similar to how current_date
can be, such that the filters will work?
E.g., my fields are strings… According to WP documentation here are the possible types available:
'string'
,'boolean'
,'integer'
,'number'
,'array'
, and'object'
Any other suggestions?
Thanks