Php Wordpress嵌套循环,无法从查询中删除meta_键
我有两个自定义帖子类型A和B,用相同的自定义分类法链接在一起。 在使用“default”循环遍历一个posts时,我希望每个A都得到具有相同分类法的所有B 代码如下所示:Php Wordpress嵌套循环,无法从查询中删除meta_键,php,wordpress,custom-post-type,nested-loops,custom-fields,Php,Wordpress,Custom Post Type,Nested Loops,Custom Fields,我有两个自定义帖子类型A和B,用相同的自定义分类法链接在一起。 在使用“default”循环遍历一个posts时,我希望每个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!
<?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;?>
我无法摆脱它们。我从未在任何地方指定过这种排序,我的B帖子也没有这个自定义字段
它在SQL查询中生成以下行:
aaaa_postmeta.meta_key = 'position'
并阻止我列出帖子。
我试着使用$args,删除tax\u查询并更改post\u类型,但总是一样的
谢谢你抽出时间 抱歉,几个小时后我才意识到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' );
现在更合乎逻辑了。
对不起打扰了
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' );