I’m not where I can test this, but for something like: www.website.com/events?date=past
, perhaps an approach like that outlined below could work.
(I can almost guarantee a type or two, so be sure to rewrite/edit/rework this into your function and ensure you don’t inherit my errors).
if (isset($_GET['date']))
{
//using past, present, future as example. not sure what you wanted to pass in url
switch( $_GET['date'] ) {
case ('past'):
$compare="<";
break;
case ('present'):
$compare="=";
break;
case ('future'):
$compare=">=";
break;
}//switch
// $metas will be array of arrays, and we only want one of those,
// otherwise we risk altering the compare value of the city or
// some other meta query array
//get existing meta_query from $query
$metas = $query->get('meta_query');
foreach ( $metas as $meta ) {
//limit edits to the one we want, when we want it
if ( $meta['key'] == 'event_start_date' && $meta['compare'] != $compare ) {
//might not need to unset, as setting it will overwrite
unset( $meta['compare'] );
$meta['compare'] = $compare;
}//if
}//foreach
//now that foreach is over, re-set() the whole meta query
$query->set( 'meta_query', $metas );
}//if (isset)
Unrelated
Also, be mindful that your
if (isset($_GET['city']))
{
$query->set('meta_key', 'city');
$query->set('meta_value', $_GET['city']);
}
}
might be overwriting an existing meta query (I’m unsure if adding it that way appends, prepends, or replaces anything being handled by WP_Meta_Query
.)
In any event, using $query->get('meta_query');
first can allow you to add another array of args to the meta_query:
$meta_query = $query->get('meta_query');
$my_new_meta_query = array(
array(
'key' => 'city',
'value' => $_GET['city'],
),
);
$meta_query[] = $my_new_meta_query;
I only add that in case your intention was to build meta queries with url params such as www.website.com/events?date=past&city=melbourne
. etc.