我需要按以下方式调整所有归档页面的主循环。我已经注册了自定义分类法“myposttypes”。每一篇文章都被分配给一个术语“myposttypes”。
如果是类别或标记存档页面,则查询结果中只应包含带有“myposttypes->term1”的帖子。
如果是自定义分类法归档页面,则查询结果中只应包含带有“myposttypes->term2”的帖子。
我已经执行了相应的pre_get_posts
钩住但I\'m not getting any results at all for all my archive pages. 在我的博客实例中,我有很多带有相应分类法的帖子,所以应该会有结果。
以下是我的方法:
function wpmw_adjust_archive_queries( $query ) {
if ( ! is_admin() && is_archive() && $query->is_main_query() ) {
if ( is_category() || is_tag() ) $term_slug = \'term1\';
if ( is_tax( \'beitragsarten\', \'news\' ) ) $term_slug = \'term2\';
$taxquery = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'myposttypes\',
\'field\' => \'slug\',
\'terms\' => $term_slug
),
),
);
$query->set( \'tax_query\', $taxquery );
}
}
add_action( \'pre_get_posts\', \'wpmw_adjust_archive_queries\' );
UPDATE:这是我经过调整和简化的方法,不幸的是,这仍然不起作用。我得到一个空结果。I var\\u dump\'ed$query和tax\\u query似乎正确添加到query\\u vars中。function wpmw_adjust_archive_queries( $query ) {
if ( ! is_admin() && $query->is_archive() && $query->is_main_query() ) {
$tax_query = $query->get( \'tax_query\' ) ? : array();
$tax_query[] = array(
\'taxonomy\' => \'myposttypes\',
\'field\' => \'slug\',
\'terms\' => array( \'term1\' ),
\'operator\' => \'IN\'
);
$query->set( \'tax_query\', $tax_query );
}
}
add_action( \'pre_get_posts\', \'wpmw_adjust_archive_queries\' );
下面是$查询的对应部分[tax_query] => Array
(
[0] => Array
(
[taxonomy] => myposttypes
[field] => slug
[terms] => Array
(
[0] => term1
)
[operator] => IN
)
)