我正在尝试使用URL参数修改我的“事件”自定义帖子类型的查询,方法是使用following:
function my_pre_get_posts($query)
{
// do not modify queries in the admin
if (is_admin())
{
return $query;
}
// only modify queries for \'event\' post type
if (isset($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'] == \'event\')
{
// allow the url to alter the query
if (isset($_GET[\'city\']))
{
$query->set(\'meta_key\', \'city\');
$query->set(\'meta_value\', $_GET[\'city\']);
}
}
// return
return $query;
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\');
当使用以下字符串修改查询时,这种方法效果很好www.website.com/events?city=melbourne
, 但在我的情况下,我希望将查询的日期范围从当前日期当天或之后开始的事件更改为当前日期之前(使用自定义字段event_start_date
). 因此,我现有的查询如下所示:$args = array(
\'post_type\' => \'event\',
\'ignore_sticky_posts\' => 1,
\'posts_per_page\' => 10,
\'post_status\' => \'publish\',
\'paged\' => get_query_var( \'paged\' ),
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'event_start_date\',
\'type\' => \'DATE\',
\'value\' => current_time(\'Ymd\'),
\'compare\' => \'>=\',
),
),
);
因此,我应该如何修改pre_get_posts
函数将按URL的查询更改为\'<=\'
当前日期?感谢您的帮助。