Wordpress嵌套循环,无法从查询中删除meta_key


Wordpress nested loop, cannot remove meta_key from the query

我有两个自定义帖子类型A和B,它们用同一个自定义分类法链接在一起。当使用"默认"循环遍历A帖子时,我希望每个A都使用相同的分类法获得所有B。

代码如下:

<?php if(have_posts()): while(have_posts()): the_post(); ?>
    <?php 
        $A_Bs=get_the_terms( $post->ID, 'A_B');
    ?>
    <?php if($A_Bs!=false && count($A_Bs)>0):?>
        <?php
            $A_B=$A_Bs[0];
            $args = array(
                'post_type'      => 'B',
                'tax_query' => array(
                  array(
                    'taxonomy' => 'A_B',
                    'field' => 'term_id',
                    'terms' => $A_B->term_id,
                  ),
                ),
            );
            $loop = new WP_Query($args);
            $saved_post=$post;
        ?>
        <?php while ($loop->have_posts()) : $loop->the_post();?>
            blabla
        <?php endwhile;?>
        <?php $post=$saved_post;?>
    <?php endif;?>
<?php endwhile; endif;?>

但是子循环总是空的。原因是,在query_var中,我有两个人:

  'meta_key' => string 'position' (length=8)
  'orderby' => string 'meta_value_num' (length=14)

我无法摆脱他们。我从来没有在任何地方指定过这个排序,我的B帖子也没有这个自定义字段。

它在SQL查询中生成以下行:

aaaa_postmeta.meta_key = 'position'

并阻止我列出帖子。我试着使用$args,删除tax_query并更改post_type,但它总是一样的。

感谢您抽出时间!

很抱歉,几个小时后我才意识到我在functions.php 中有以下内容

function order_by_position($query){
    if(is_post_type_archive( 'A')||is_post_type_archive( 'C')||is_post_type_archive( 'D')){
        $query->query_vars['meta_key']="position";
        $query->query_vars['orderby']="meta_value_num";
    }
    return $query;
}
add_action( 'pre_get_posts', 'order_by_position' );

现在更符合逻辑了。抱歉打扰了。