Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Last active December 1, 2021 10:48
Show Gist options
  • Save zeshanshani/842c14baeb42d33ce90d1d1bf7a5de68 to your computer and use it in GitHub Desktop.
Save zeshanshani/842c14baeb42d33ce90d1d1bf7a5de68 to your computer and use it in GitHub Desktop.
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'
),
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment