The following arguments are used for
WP_Query to check if a reservation for a game can be created.
Meta input fields for post are created using
ACF DateTime Picker and set to
$start_date = '2019-08-31 12:15:00'; $end_date = '2019-08-31 12:20:00'; $args = array( 'post_type' => 'rezervacija', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'AND', array( 'relation' => 'OR', array( 'key' => 'start_date', 'value' => array($start_date, $end_date), 'compare' => 'BETWEEN', 'type' => 'DATETIME' ), array( 'key' => 'end_date', 'value' => array($start_date, $end_date), 'compare' => 'BETWEEN', 'type' => 'DATETIME' ) ), array( 'key' => 'location', // Look also for specific playground, not important for this question 'value' => 3, 'compare' => '=' ) ) ); $query = new WP_Query($args);
This code generally works, but not fully as expected.
If there is already reservation created with a start date set to 12:00 (
2019-08-31 12:00:00) and end time 14:00 (
2019-08-31 14:00:00) and then if a new user selects time 12:15, and end time 13:30, in that case since it looks for those values separately the query won't find anything but it should. So generally this query won't find data properly when the time is somewhere inside of previous reservation times.
How can I modify this query to search for posts whose $start_date and $end_date are anywhere between values start_date and end_date?