Skip to content

Instantly share code, notes, and snippets.

@isotrope
Last active October 18, 2019 15:26
Show Gist options
  • Save isotrope/f836750c6fa64186a8d16021147149a8 to your computer and use it in GitHub Desktop.
Save isotrope/f836750c6fa64186a8d16021147149a8 to your computer and use it in GitHub Desktop.
<?php
$query_args = array(
'meta_query' => array(
// relation between time query and online query
'relation' => 'OR',
// time query
array(
// relation for time query
'relation' => 'AND',
array(
'key' => 'start_time',
'value' => '20191018100000',
'compare' => '>=',
),
array(
'key' => 'end_time',
'value' => '20191018120000',
'compare' => '<=',
),
),
// online query
array(
'key' => 'online',
'value' => 'true',
'compare' => '=',
),
), // end meta_query
);
@cgrymala
Copy link

Almost; except that start_time and end_time are time values, not full datetime values, so, the value would look more like 12:00:00 or 13:59:48, and the type parameter for those 2 queries is set to TIME.

So:

array(
	'key'     => 'start_time',
	'value'   => '20191018100000',
	'compare' => '>=',
),

becomes:

array(
	'key'     => 'start_time',
	'value'   => '10:00:00',
	'compare' => '>=',
	'type' => 'TIME', 
),

@cgrymala
Copy link

This seems to work:

$meta_query['time-range'] = $meta_query['time-range'] = array(
	'relation' => 'AND',
	array(
		'key'     => 'hsucs_start_time',
		'value'   => array( '00:00:01', $t['start'] . ':00' ),
		'compare' => 'NOT BETWEEN',
	),
	array(
		'key'     => 'hsucs_end_time',
		'value'   => array( $t['end'] . ':00', '23:59:59' ),
		'compare' => 'NOT BETWEEN',
	)
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment