I'm having a search form for rentals that search locations available according to a date range.
For now, I store all reserved dates for each posts as post_meta. For exemple ;
post_id: 231 post_meta : location_reservation_start : 2022-09-02
post_id: 231 post_meta : location_reservation_end : 2022-09-08
post_id: 231 post_meta : location_reservation_start : 2022-10-02
post_id: 231 post_meta : location_reservation_end : 2022-09-02
(multiple meta_key for the same post)
There is the query :
$start_date = $dates[0] ;
$end_date = $dates[1] ;
$date_query[] = array(
'key' => 'location_reservation_start',
'value' => array($start_date , $end_date ) ,
'compare' => 'NOT BETWEEN',
'type' => 'DATE'
) ;
$date_query[] = array(
'key' => 'location_reservation_end',
'value' => array($start_date , $end_date ) ,
'compare' => 'NOT BETWEEN',
'type' => 'DATE'
) ;
array_push($meta_query, $date_query );
This code work if there's only one post_meta for post. As long as the post have multiple same meta_key, the NOT BETWEEN doesn't seem to work.
There is a way to make it work like that or should I store the reservation dates in a post_meta serialized as an array?
Thanks
NOT
style queries are extremely expensive, even if you get this working it won't scale as your site grows, and there's a very high chance you won't be able to handle more than one or two concurrent users on that page. You also need to store the dates in a MySQL friendly format. You should avoid serialized arrays as that means math and date comparisons will no longer be available to you inWP_Query