Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress嵌套循环,无法从查询中删除meta_键_Php_Wordpress_Custom Post Type_Nested Loops_Custom Fields - Fatal编程技术网

Php Wordpress嵌套循环,无法从查询中删除meta_键

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!

我有两个自定义帖子类型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!=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' );