I am building a Wordpress site with pages, posts and events (with multiple dates you can sign in). I am new to Wordpress so I was looking for ideal solution for this events.
I believe the best solution is to create custom post type called "Event" and then handle it separately.
I am not sure however how to add multiple dates to each event. I have been reading about custom taxonomies and post metadata but every example I went through was like creating a custom category, tag or adding a single value to the post.
What would you recommend for adding multiple dates (dont know how many in advance) to a custom post type? Is it taxonomy or post metadata? Is there a metadata of type date with build-in date input validation or datepicker?
Thank you!
Now, In Detail
Add Meta Box & Set Multi Select Datepicker in its HTML. Use jQuery date picker multi select and unselect to add multi select date picker.
function myplugin_add_meta_box() {
add_meta_box(
'event-date',
'Set Event Dates',
'myplugin_meta_box_callback',
'event'
);
}
function myplugin_meta_box_callback(){
// Set HTML here which you want to see under this meta box.
// Refer https://stackoverflow.com/questions/17651766/jquery-date-picker-multi-select-and-unselect
// for multiselect datebox.
echo '<input type="date" id="datePick" name=save-dates/>';
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_save_postdata($post_id){
if ( 'event' == $_POST['post_type'] ) {
update_post_meta($post_id, 'save-dates', sanitize_text_field( $_REQUEST['save-dates'] ));
}
}
add_action( 'save_post', 'myplugin_save_postdata' );
Now, wherever you want to show these dates. Just retrieve them from database and start using them.
get_post_meta($post_id, 'save-dates',true);
Don't forget to explode(',', get_post_meta($post_id, 'save-dates',true)) before using them. As dates are being saved in comma format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With