I currently have a metabox with a custom date field but I'm wondering how do I customize it to save multiple dates instead of just one? (I need it to function similar to the tags metabox)?
1 Answer
In CMB2 you can use the group
type to create a repeating fields, then inside the group, you declare the text_date
type.
Let's say you have an Events where you set multiple dates.
$events = new_cmb2_box([
'id' => 'events_metabox',
'title' => __('Events', 'text_domain'),
'object_types' => ...,
'show_on' => ...,
...
]);
$events_group_id = $events->add_field([
'id' => 'events',
'type' => 'group',
'desc' => ...,
'options' => [
'group_title' => __('Event Date {#}', 'text_domain'),
'add_button' => __('New Event Date', 'text_domain'),
'remove_button' => __('Remove Event Date', 'text_domain')
]
]);
$events->add_group_field($events_group_id, [
'id' => 'event_text_date',
'name' => __('Event Date', 'text_domain'),
'desc' => ...,
'type' => 'text_date'
]);
-
This works, except instead of adding a new box each time, I want it to just add the data. How do I do that? Say my user wants to add 500 dates to their post...that's going to end up being a lot of metaboxes.– abc0213Commented Apr 12, 2018 at 13:33
-
That's the only possible way in CMB2, if you want the same functionality like tags the only workaround is custom code your metabox. Commented Apr 12, 2018 at 17:49