我不是在哪里可以测试这个,而是为了:www.website.com/events?date=past
, 也许下面概述的方法可以奏效。
(我几乎可以保证有一两种类型,所以请确保将其重写/编辑/重做到您的函数中,并确保您不会继承我的错误)。
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)
此外,请注意
if (isset($_GET[\'city\']))
{
$query->set(\'meta_key\', \'city\');
$query->set(\'meta_value\', $_GET[\'city\']);
}
}
可能正在覆盖现有的元查询(我不确定以这种方式添加它是否会附加、前置或替换正在处理的任何内容
WP_Meta_Query
.)在任何情况下,使用
$query->get(\'meta_query\');
首先,您可以将另一个参数数组添加到meta\\u查询:
$meta_query = $query->get(\'meta_query\');
$my_new_meta_query = array(
array(
\'key\' => \'city\',
\'value\' => $_GET[\'city\'],
),
);
$meta_query[] = $my_new_meta_query;
我只想补充一下,如果您的意图是使用url参数构建元查询,例如
www.website.com/events?date=past&city=melbourne
. 等