I am using The Events Calendar and (correct me if I’m wrong) there is no checking to see if an event has a time conflict with another event that’s already been entered. For example, if there is already an event with a certain start time in a category, there cannot be another event posted with the same start time and in the same category. That would be a conflict.
Is there any way I can check values in the database for start time and category when the event post is submitted, and if both those values already exist for a post, kick the user back to the Add Event page?
EDIT: Here is what I am attempting to use in the functions.php file as the original save_post hook:
add_action('save_post', 'nu_time_conflict');
function nu_time_conflict($post_id) {
$prevent_publish = NULL;
if (isset($_REQUEST['EventStartDate']) && isset($_REQUEST['EventStartHour']) && isset($_REQUEST['EventStartMinute'])) {
$start_date = date( TribeDateUtils::DBDATETIMEFORMAT, strtotime($_REQUEST['EventStartDate'] . " " . $_REQUEST['EventStartHour'] . ":" . $_REQUEST['EventStartMinute'] . ":00") );
} else {
die('Ingen datum valde.');
}
if (isset($_REQUEST['EventEndDate']) && isset($_REQUEST['EventEndHour']) && isset($_REQUEST['EventEndMinute'])) {
$end_date = date( TribeDateUtils::DBDATETIMEFORMAT, strtotime($_REQUEST['EventEndDate'] . " " . $_REQUEST['EventEndHour'] . ":" . $_REQUEST['EventEndMinute'] . ":59") );
} else {
die('Ingen datum valde.');;
}
//Query post type 'tribe_events' to check for posts whose meta values match the POSTed ones
$args = array(
'post__not_in'=> array($post_id),
'post_type' => 'tribe_events',
'post_status' => array('publish','pending','future','private'),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'EventStartDate',
'value' => array(strtotime($start_date),strtotime($end_date)),
'compare' => 'BETWEEN'
),
array(
'key' => 'EventEndDate',
'value' => array(strtotime($start_date),strtotime($end_date)),
'compare' => 'BETWEEN'
)
)
);
$existingMeta = get_posts( $args );
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if(empty($existingMeta)){
remove_action('save_post', 'nu_time_conflict');
wp_update_post(array('ID' => $post_id, 'post_status' => 'pending'));
$current_user = wp_get_current_user();
$user_mail = $current_user->user_email;
$admin_email = get_option('admin_email');
$type = get_post_type( $post_id );
if (( !wp_is_post_revision( $post_id )) && ($type = 'tribe_events' )) {
$to = array($user_mail, $admin_email);
$subject = 'Ny bokning';
$message = $current_user->user_firstname.' '.$current_user->user_lastname.' har skapat en ny bokning.<br>';
$message .= 'För att redigera gå in på ' .get_permalink($post_id).'<br><br>';
$message .= '<em><small>OBS! Bokningen är inte giltig utan att bli bekräftad av administratören.</small></em>';
wp_mail($to, $subject, $message );
}
add_action('save_post', 'nu_time_conflict');
$prevent_publish = FALSE;
} else {
$prevent_publish = TRUE;
}
if ($prevent_publish) {
remove_action('save_post', 'nu_time_conflict');
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
add_action('save_post', 'nu_time_conflict');
}
}
And here would be my redirect and custom message:
add_filter('redirect_post_location','nu_redirect_location',10,2);
function nu_redirect_location($location,$post_id){
if (isset($_POST['publish'])) {
$status = get_post_status( $post_id );
if($status=='draft'){
$location = add_query_arg('message', 10, $location);
}
}
return $location;
}
add_filter('post_updated_messages', 'nu_custom_messages');
function nu_custom_messages($messages){
$messages['tribe_events'][10] = 'Den valda tiden är inte tillgänglig. Välj ny tid';
return $messages;
}
It either isn’t doing the comparison to check if those dates exist, or there is something wrong with my IF statement. It thinks all entries are valid and sends emails, and I am purposefully putting wrong ones in. So the posts aren’t being marked as drafts, and the message is the one for a successful post. (The emails are also sent when the page is loaded, before any data is sent. What is with that? Should I not be using save_post?)
Any help is greatly appreciated. Since The Events Calendar has no availability function as far as I can tell, getting this to work would be perfect.