我有一个带有自定义查询的页面模板:
<?
$filter = explode(\' \', $_GET[\'filter\']); // "page/?filter=taxonomy+term"
$paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
$query = new WP_Query(array(
\'post_type\' => \'articles\',
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => $filter[0],
\'terms\' => $filter[1],
\'field\' => \'slug\'
)
)
));
这很好,但当没有过滤器时,我不想查询分类法,所以我考虑这样做:
$query = new WP_Query(array(
\'post_type\' => \'articles\',
\'paged\' => $paged,
));
if (! empty($filter[0])) {
$query->set(\'tax_query\', array(
array(
\'taxonomy\' => $filter[0],
\'terms\' => $filter[1],
\'field\' => \'slug\'
)
));
}
但它不像以前那样工作,当没有过滤器时,它什么也不显示,当有任何过滤器时,它只显示所有内容。因此,我猜查询中没有设置分类法。我做错了什么?