Php 相关产品查询类别和属性

Php 相关产品查询类别和属性,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我希望woocommerce显示与类别和属性匹配的相关产品。当我查看与get_相关的函数时,我不确定如何将pa_city与变量$city匹配添加到查询中。我尝试过这个钩子,但它仍然只匹配类别,查询中没有使用城市,不知道为什么: add_filter( 'woocommerce_product_related_posts', 'relate_city' ); function relate_city() { $city = isset( $_COOKIE['newci

我希望woocommerce显示与类别和属性匹配的相关产品。当我查看与get_相关的函数时,我不确定如何将pa_city与变量$city匹配添加到查询中。我尝试过这个钩子,但它仍然只匹配类别,查询中没有使用城市,不知道为什么:

add_filter( 'woocommerce_product_related_posts',
           'relate_city' );
function relate_city() {
    $city = isset( $_COOKIE['newcity'] ) ? $_COOKIE['newcity'] : 'not set';
    $get_related_products_args = array(
     'orderby' => 'rand',
     'posts_per_page' => $limit,
     'post_type' => 'product',
     'fields' => 'ids',
     'meta_query' => $meta_query,
     'tax_query' => array(
       'relation' => 'AND',
       array(
         'taxonomy' => 'product_cat',
         'field' => 'id',
         'terms' => $cats_array
       ),
       array(
         'taxonomy' => 'pa_city',
         'field' => 'slug',
         'terms' => array ($city)
       )
     )
    );
    return $get_related_products_args;
}
任何帮助都将不胜感激

试试这个

add_filter( 'woocommerce_product_related_posts', 'relate_city' ); function relate_city() { $city = isset( $_COOKIE['newcity'] ) ? $_COOKIE['newcity'] : 'not set'; $get_related_products_args = array( 'orderby' => 'rand', 'posts_per_page' => $limit, 'post_type' => 'product', 'fields' => 'ids', 'meta_query' => $meta_query, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $cats_array ), array( 'key' => 'pa_city', 'value' => $city, 'compare' => '=' ) ) ); return $get_related_products_args; } 添加过滤器('woocommerce\u product\u related\u posts', ‘联系城市’; 功能相关城市(){ $city=isset($_COOKIE['newcity'])?$_COOKIE['newcity']:'notset'; $get\u related\u products\u args=数组( 'orderby'=>'rand', “每页帖子数”=>$limit, “post_类型”=>“产品”, “字段”=>“ID”, “meta\u query”=>$meta\u query, “tax_query”=>数组( '关系'=>'和', 排列( “分类法”=>“产品分类”, “字段”=>“id”, “术语”=>$cats\u数组 ), 排列( “关键”=>“帕乌市”, “值”=>$city, '比较'=>'=' ) ) ); 返回$get\u相关产品\u参数; }
为什么你在
$terms
中循环,但在第一个之后立即
break
。那是我测试时留下的。我把它拿走了。类别和不起作用,也不在中发布。我在这里肯定错过了一个概念。