zeshanshani
2/2/2017 - 10:05 AM

WP_Query 'meta_query' for "Date Start" and "Date End" custom fields. Conditions are (relation = 'OR'): 1. If "Date End" does not exist, com

WP_Query 'meta_query' for "Date Start" and "Date End" custom fields. Conditions are (relation = 'OR'):

  1. If "Date End" does not exist, compare "Date Start" only.
  2. If "Date End" exists, show events in between that and "Date Start".
<?php

/**
* WP_Query 'meta_query' for "Date Start" and "Date End" custom fields. 
* Conditions are (relation = 'OR'):
*   1. If "Date End" does not exist, compare "Date Start" only.
*   2. If "Date End" exists, show events in between that and "Date Start". 
*/

'meta_query' => array(
  'relation' => 'OR',
  array(
    'key'     => 'date_start',
    'value'   => date( 'Ymd' ),
    'compare' => '=',
    'type'    => 'date'
  ),
  array(
    'relation' => 'AND',
    array(
      'key'     => 'date_start',
      'value'   => date( 'Ymd' ),
      'compare' => '<=',
      'type'    => 'date'
    ),
    array(
      'key'     => 'date_end',
      'value'   => date( 'Ymd' ),
      'compare' => '>=',
      'type'    => 'date'
    ),
  ),
)