Complex date range with WP_Query and BETWEEN

It’s a little hard to think in meta_query code for complicated case like this and without access to data. 🙂

What you have working can be expressed as two following conditions:

  • start_date BETWEEN event_begin and event_end
  • end_date BETWEEN event_begin and event_end

Your first case with both in between seems irrelevant, since these two will catch it anyway.

The challenge you are having with the rest of it is that you want reverse logic — event_begin or event_end BETWEEN start_date and end_date. But you cannot express it in same way since those are unique to each event.

But you don’t really care when they are, just that they around search dates.

So two more conditions can be expressed as:

  • ( start_date <= event_begin ) AND ( end_date >= event_begin ) (event dates at or around search start)
  • ( start_date <= event_end ) AND ( end_date >= event_end ) (event dates at or around search end)

Again, I struggle to write this as meta query “in theory”, but since meta queries can be nested you should be able to stuff this all into their logic.

PS Performance might become a consideration and call for aggregate of multiple simpler queries rather than single complex one.